4

I read a lot about this issue but I was not able to figure it out. I am using HttpURLConnection and it works fine, but on JellyBean where it throws the IOException: No Authentication challenge found when I call getResponseCode() in case of 401 even though the backend is sending in the response the correct headers:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: realm="realm" 

On the other hand, debugging it, I experienced something strange. I printed the headers value out on the console this way:

    Map<String, List<String>> map = connection.getHeaderFields();
    if (map != null) {
            Set<String> set = map.keySet();
            for (String tmp : set) {
                Log.i(LOG_TAG, tmp != null ? tmp : "null");
            }
    }

just before calling connection.getResponseCode(); and it stopped to throw that exception.

Any clue?

The connection is set up with the following values:

    connection.setDoInput(true);
    connection.setDoOutput(true);     
    connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
    connection.setRequestProperty("Accept", "application/json");
    connection.setRequestProperty("Accept-Charset", "utf-8");
    connection.setRequestProperty("Accept-Encoding", "gzip");
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • you only get 401 in JellyBean? – dwbrito May 29 '14 at 17:21
  • And Also in ice cream sandwich – Blackbelt May 29 '14 at 17:22
  • Check if for any reason in those version any header is missing. A nice exercise would be to make the request in an application like 'Advance Rest Client' (for chrome) and check if the result is 200. This should be easy to experiment. Then, if it is, compare your request to the one you have in the android application and see if anything differs. – dwbrito Jun 02 '14 at 10:10
  • I actually checked. The header is congruent with the rfc – Blackbelt Jun 02 '14 at 10:12
  • And in Advanced Rest Client you get 200 or 401 ? – dwbrito Jun 02 '14 at 10:16
  • I can't use any other http client. The issue is related to `HttpURLConnection` and in those specific versions of Android, – Blackbelt Jun 02 '14 at 10:17
  • What I am saying is that you try and use this application and confirm that you get a 200 and not a 401. https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo After you check its effectively 200, to debug the android application on the moment the request is executed and to compare the headers from both – dwbrito Jun 02 '14 at 10:21
  • but it actually has to return `401`, what I was not expecting was the exception – Blackbelt Jun 02 '14 at 10:22

0 Answers0