No authentication found

I read a lot about this problem, but I could not figure it out. I use HttpURLConnection, and it works fine, but on JellyBeanwhere it issues IOException: No Authentication challenge foundwhen I call getResponseCode()in the event 401, although the backend sends the correct headers in response:

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

On the other hand, while debugging it, I experienced something strange. I printed the value of the headers on the console as follows:

    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");
            }
    }

before the call connection.getResponseCode();, and he stopped to throw this exception.

Any clue?

The connection is configured 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");
+4
source share

Source: https://habr.com/ru/post/1530574/


All Articles