Skip to content Skip to sidebar Skip to footer

Java.io.ioexception: Received Authentication Challenge Is Null

I need get a response code, but it's throw a IOException. I don't know what's the matter! try { url = new URL(urlBuilder.toString()); conn = (HttpURLConnec

Solution 1:

Most often, you can get a response code by calling .getResponseCode() on your connection a second time. This was confusing for me at first, but if you read HttpURLConnectionImpl enough times your eyes will stop bleeding and see the truth...

If you modify your code in this way, you will get the response code:

try
    {
        url = new URL(urlBuilder.toString());
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setConnectTimeout(TIME_OUT);
        conn.setRequestMethod(METHOD_GET);
        conn.setRequestProperty("accept", "*/*");
        conn.connect();
        int responseCode = conn.getResponseCode();   //throw IOException:Received authentication challenge is nullif (responseCode == HTTP_OK)
        {
            inStream = conn.getInputStream();
            response = getResponse(inStream);
        }
        else
        {
            response = "response code:"+responseCode;
        }
    } catch (Exception e)
    {
        // Here you can get the correct response codeif (conn != null) {
            int responseCodeAfterException = conn.getResponseCode();
            // Handle according to new response code
        }
        // Maybe don't throw e if 401?throw e;
    }
    finally
    {
        conn.disconnect();
    }
    return response;
}

See responseCodeAfterException.

Solution 2:

If you can make changes to server side you could make sure you have correct headers sent with 401 status code. From http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2

10.4.2 401 Unauthorized

The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response, since that entity might include relevant diagnostic information. HTTP access authentication is explained in "HTTP Authentication: Basic and Digest Access Authentication" [43].

I was using same code as @tj-thind until I ran into situation described by @Chiara. Then I added WWW-Authenticate and Content-Length headers to my server script and now I can get correct 401 status code without IOException when using getResponseCode().

See also IOException: "Received authentication challenge is null" (Apache Harmony/Android)

Solution 3:

I've come across this issue myself before. That exception is actually being thrown by getResponseCode in the event of a 401, which is the HTTP code for Unauthorized. This seems to be unique to the Android implementation of HttpURLConnection. The easiest way to deal with it, I find, is just to deal with 401's in the catch block.

catch (java.io.IOException e) {
    //Probably got a 401 here
    response = "response code:"+401;
}

Post a Comment for "Java.io.ioexception: Received Authentication Challenge Is Null"