Wednesday, August 1, 2012

[android-developers] HttpsURLConnection not returning 401

Hey!

I'm wondering if anyone could help me, or suggest alternatives for the problem below:

I've run into what seems to be a very strange problem. I'm using the HttpsURLConnection class in my Android app, and doing authentication using the Authenticator class. I'm using this to connect to our web service and pull down some JSON.

I can connect to the web service with inputted username and password, and get a JSON response back. If I enter valid credentials this works absolutely fine and I get a 200 response back. However if I give it invalid credentials then then I never get a response and the my Async Task (that's running the code) never completes. I have a dialog that displays and this never goes away, and there's not additional logging happening after the connection is made.

The code that is doing the connection is as follows:

private static String retrieveStream(String url) throws SocketTimeoutException {

   
String streamContent = "";
   
HttpsURLConnection urlConnection = null;

   
try {

       
Log.d("Connection", "Connecting to: " + url);

       
Log.d("Connection", "Opening connection");
        urlConnection
= (HttpsURLConnection) new URL(url).openConnection();
       
Log.d("Connection", "Setting connect timeout");
        urlConnection
.setConnectTimeout(5000);
       
Log.d("Connection", "Setting read timeout");
        urlConnection
.setReadTimeout(5000);
       
Log.d("Connection", "Setting Allow All certs (because this is just testing)");
        urlConnection
.setHostnameVerifier(new AllowAllHostnameVerifier());

       
Log.d("Connection", "Connection Response: " + urlConnection.getResponseCode() + " " + urlConnection.getResponseMessage());

       
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            streamContent
= readStreamFromConnection(urlConnection);
           
Log.d("Connection", "Returned content is: " + streamContent);
       
}

   
} catch (MalformedURLException e) {
       
Log.e("Error", "MalformedURLException thrown in retrieveStream: " + e);
   
} catch (IOException e) {
       
Log.e("Error", "IOException thrown in retrieveStream for " + url + " : " + e);
   
} finally {
        urlConnection
.disconnect();
   
}

   
return streamContent;
}
If I then run this code with invalid credentials this is what I get in the logs:
  
07-30 15:52:57.040: DEBUG/Connection(16000): Connecting to: https://webservice-that-i-use
07-30 15:52:57.040: DEBUG/Connection(16000): Opening connection
07-30 15:52:57.040: DEBUG/Connection(16000): Setting connect timeout
07-30 15:52:57.040: DEBUG/Connection(16000): Setting read timeout
07-30 15:52:57.040: DEBUG/Connection(16000): Setting Allow All certs (because this is just testing)
...
and nothing else it just hangs here until I force stop the app or redeploy. I don't get any exceptions or ANRs. If I pass in valid credentials this completes fine and I get my JSON back.

Does anyone have any ideas of why this happens? How I can get the connection to timeout on invalid credentials with HttpsURLConnection? It's almost as if the connection is just constantly trying to pass the credentials again, and again, and again, and again...

Any help would be appreciated. I should also note that my colleague has this working fine in iOS so it's not a server issue. This is running with the ICS SDK on a Asus Transformer TF101.

Thanks for reading!

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment