Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Application Requests

Application Requests Request Apps or provide ideas for the devs here.



Reply
 
LinkBack Thread Tools
Old July 7th, 2010, 02:40 AM   #1 (permalink)
New Member
 
Join Date: Jul 2010
Posts: 1
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Digest Authentication not supported in Android

I'm using URLConnection to connect to the main server. The server implements digest authentication. If I connect to the server with java library, the connection is successful. But if I use the same code for android, the connection is rejected for the reason - username and password do not match.

Here is the code for my Java project:



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;

public class Connect {

static StringBuilder sb;

public static void main(String args[]) {
String strURL = "http://hostserver/";
final String username = "username";
final String password = "password";

Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication (username, password.toCharArray());
System.out.println(pa.getUserName() + ":" + new String(pa.getPassword()));
return pa;
}
});
BufferedReader in = null;
StringBuffer sb = new StringBuffer();

try {
URL url = new URL(strURL);
URLConnection connection = url.openConnection();
in = new BufferedReader(new InputStreamReader(connection
.getInputStream()));

String line;

while ((line = in.readLine()) != null) {
sb.append(line);
}
} catch (java.net.ProtocolException e) {
sb.append("User Or Password is wrong!");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
System.out.println("Exception");
}
}

System.out.println("The Data is: " + sb.toString());

}
}




The above code works fine and I'm able to connect to my host server which is implementing digest authentication. I'm unable to use the same code for Android to connect. Here is my android code:



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Connect extends Activity {
static StringBuilder sb;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String strURL = "http://hostserver/";
final String username = "username";
final String password = "password";
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication (username, password.toCharArray());
// System.out.println(pa.getUserName() + ":" + new String(pa.getPassword()));
return pa;
}
});
BufferedReader in = null;
StringBuffer sb = new StringBuffer();

try {
URL url = new URL(strURL);
URLConnection connection = url.openConnection();
in = new BufferedReader(new InputStreamReader(connection
.getInputStream()));

String line;

while ((line = in.readLine()) != null) {
sb.append(line);
}
} catch (java.net.ProtocolException e) {
sb.append("User Or Password is wrong!");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
System.out.println("Exception");
}
}

Log.d("DATA", sb.toString());

}
}



If the same code works for Java, it should also work for Android.

The code loops in the Authenticator as it finds username and password not matching in the Android code for some reason which in fact are correct. The code runs perfect for Java project.

dbhasin is offline  
Reply With Quote
Sponsors
Reply

Bookmarks

Tags
android, authentication, authenticator, digest, http, urlconnection


Go Back   Android Forums > Android Development > Application Development > Application Requests User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: How to list all fonts supported by Android lbj05 Android Applications 0 September 13th, 2010 02:53 AM
Golf Digest GPS lexluthor Android Applications 3 August 12th, 2010 10:42 AM
SSL 2 authentication and Android? moijk Android Lounge 0 April 19th, 2010 05:38 AM
Android OS not supported on website eghosh HTC Hero (Sprint) 6 October 31st, 2009 01:02 PM



All times are GMT -5. The time now is 02:49 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo