Rectangle 27 1

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found?


DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(userName,userPass));
HttpGet request = new HttpGet();
request.addHeader("Accept", "application/xml");
request.setURI(new URI(service));
HttpResponse response = client.execute(request);

I have fixed the problem for the Jelly bean. Please use the below code for the above scenario

you got the proper response as you needed.

Note
Rectangle 27 1

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found?


401 Unauthorized
407 Proxy Authentication Required
header('WWW-Authenticate: OAuth realm="users"');
header('HTTP/1.1 401 Unauthorized');
java.io.IOException: No authentication challenges found

If you own the server-side API, then you can fix it by adding the required WWW-Authenticate header when you return 401 or 407. In my case, I fixed it in PHP as follows:

The 401 (Unauthorized) response message is used by an origin server to challenge the authorization of a user agent. This response MUST include a WWW-Authenticate header field containing at least one challenge applicable to the requested resource.

Note
Rectangle 27 1

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found?


401 Unauthorized
407 Proxy Authentication Required
header('WWW-Authenticate: OAuth realm="users"');
header('HTTP/1.1 401 Unauthorized');
java.io.IOException: No authentication challenges found

If you own the server-side API, then you can fix it by adding the required WWW-Authenticate header when you return 401 or 407. In my case, I fixed it in PHP as follows:

The 401 (Unauthorized) response message is used by an origin server to challenge the authorization of a user agent. This response MUST include a WWW-Authenticate header field containing at least one challenge applicable to the requested resource.

Note
Rectangle 27 1

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found?


+1 For a clear explanation and the links to the code. The code also expects realm=" to be the first parameter after the scheme.

+1 for Excellent response. Problem on my side was the server return of realm=... instead of realm="..." . Fixed that and app started working on both ICS and JB

As one can see in 4.1 JB the method getAuthorizationCredentials() throws the IOException. It parses the challenge headers it finds in the response using HeaderParser.parseChallenges(..), if the response code is 401 or 407. If the returned List is empty the Exception is thrown.

By now we have been able to verify that the usage of realm= without quotation marks was indeed the reason which caused this error for us. We were able to change this on our backend side and now everything is working as expected.

I am currently facing the same problem. On 4.1 Jelly Bean I receive an IOException "No authentication challenges found" when calling getResponseCode() on the HttpURLConnection.

I made my app work under android 4.x finally by just removing mHttpConnection.setDoOutput(true);But I still had not knewn clearly about the root cause.

We are currently investigating what exactly causes that List to be empty, but have the suspicion that our server might use realm=... instead of realm="..." in the challenge header. Missing quotation marks might be the cause for this problem. We have to investigate further if that is indeed the case and if we can make it work.

did you find a workaround for this? It works on kitkat, but not on JB

Note
Rectangle 27 1

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found?


try
{
    responseStream = new BufferedInputStream(connection.getInputStream());
}
catch(IOException e)
{
    responseStream = new BufferedInputStream(connection.getErrorStream());
}

I have the same problem. I found this workaround, but it is not working on Android 2. On Jelly Bean, it works fine. Just use getErrorStream() instead of getInputStream().

Note
Rectangle 27 1

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found?


+1 For a clear explanation and the links to the code. The code also expects realm=" to be the first parameter after the scheme.

+1 for Excellent response. Problem on my side was the server return of realm=... instead of realm="..." . Fixed that and app started working on both ICS and JB

As one can see in 4.1 JB the method getAuthorizationCredentials() throws the IOException. It parses the challenge headers it finds in the response using HeaderParser.parseChallenges(..), if the response code is 401 or 407. If the returned List is empty the Exception is thrown.

By now we have been able to verify that the usage of realm= without quotation marks was indeed the reason which caused this error for us. We were able to change this on our backend side and now everything is working as expected.

I am currently facing the same problem. On 4.1 Jelly Bean I receive an IOException "No authentication challenges found" when calling getResponseCode() on the HttpURLConnection.

I made my app work under android 4.x finally by just removing mHttpConnection.setDoOutput(true);But I still had not knewn clearly about the root cause.

We are currently investigating what exactly causes that List to be empty, but have the suspicion that our server might use realm=... instead of realm="..." in the challenge header. Missing quotation marks might be the cause for this problem. We have to investigate further if that is indeed the case and if we can make it work.

did you find a workaround for this? It works on kitkat, but not on JB

Note
Rectangle 27 1

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found?


try
{
    responseStream = new BufferedInputStream(connection.getInputStream());
}
catch(IOException e)
{
    responseStream = new BufferedInputStream(connection.getErrorStream());
}

I have the same problem. I found this workaround, but it is not working on Android 2. On Jelly Bean, it works fine. Just use getErrorStream() instead of getInputStream().

Note
Rectangle 27 1

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found?


ICS
Jelly Bean
Note
Rectangle 27 1

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found?


// enable VM-wide cookie support for HttpUrlConnection
// see http://developer.android.com/reference/java/net/HttpURLConnection.html for details
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);

I ran into a similar issue with a web service that required cookies to operate correctly. Apparently Jelly Bean doesn't automatically create a cookie store by default (unlike previous versions), so the service wasn't able to find my session and threw a 401 every time I tried to access it. Adding the following lines of code to my application initialization fixed the problem:

Note
Rectangle 27 1

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found?


// enable VM-wide cookie support for HttpUrlConnection
// see http://developer.android.com/reference/java/net/HttpURLConnection.html for details
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);

I ran into a similar issue with a web service that required cookies to operate correctly. Apparently Jelly Bean doesn't automatically create a cookie store by default (unlike previous versions), so the service wasn't able to find my session and threw a 401 every time I tried to access it. Adding the following lines of code to my application initialization fixed the problem:

Note