java.io.IOException: Received authentication challenge is null

99封情书 提交于 2019-12-02 01:25:48

问题


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 = (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 null
        if (responseCode == HTTP_OK)
        {
            inStream = conn.getInputStream();
            response = getResponse(inStream);
        }
        else
        {
            response = "response code:"+responseCode;
        }
    } catch (Exception e)
    {
        throw e;
    }
    finally
    {
        conn.disconnect();
    }
    return response;
}

the IOException is :

05-03 20:14:01.577: WARN/System.err(1515): java.io.IOException: Received authentication challenge is null
05-03 20:14:01.596: WARN/System.err(1515):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1694)
05-03 20:14:01.577: INFO/QQWeiBo(1515): Received authentication challenge is null
05-03 20:14:01.577: WARN/System.err(1515): java.io.IOException: Received authentication challenge is null
05-03 20:14:01.577: WARN/System.err(1515): java.io.IOException: Received authentication challenge is null
05-03 20:14:01.596: WARN/System.err(1515):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1694)
05-03 20:14:01.596: WARN/System.err(1515):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
05-03 20:14:01.596: WARN/System.err(1515):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1374)
05-03 20:14:01.596: WARN/System.err(1515):     at com.szy.weibo.service.SyncHttp.httpGet(SyncHttp.java:72)
05-03 20:14:01.596: WARN/System.err(1515):     at com.szy.weibo.service.Weibo.getRequestToken(Weibo.java:45)
05-03 20:14:01.606: WARN/System.err(1515):     at com.szy.weibo.MainActivity.startWebView(MainActivity.java:95)
05-03 20:14:01.606: WARN/System.err(1515):     at com.szy.weibo.MainActivity.authorization(MainActivity.java:83)
05-03 20:14:01.606: WARN/System.err(1515):     at com.szy.weibo.MainActivity$1.onClick(MainActivity.java:71)
05-03 20:14:01.606: WARN/System.err(1515):     at android.view.View.performClick(View.java:2408)
05-03 20:14:01.596: WARN/System.err(1515):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1694)
05-03 20:14:01.596: WARN/System.err(1515):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
05-03 20:14:01.596: WARN/System.err(1515):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1374)
05-03 20:14:01.596: WARN/System.err(1515):     at com.szy.weibo.service.SyncHttp.httpGet(SyncHttp.java:72)
05-03 20:14:01.596: WARN/System.err(1515):     at com.szy.weibo.service.Weibo.getRequestToken(Weibo.java:45)
05-03 20:14:01.606: WARN/System.err(1515):     at com.szy.weibo.MainActivity.startWebView(MainActivity.java:95)
05-03 20:14:01.606: WARN/System.err(1515):     at com.szy.weibo.MainActivity.authorization(MainActivity.java:83)
05-03 20:14:01.606: WARN/System.err(1515):     at com.szy.weibo.MainActivity$1.onClick(MainActivity.java:71)
05-03 20:14:01.606: WARN/System.err(1515):     at android.view.View.performClick(View.java:2408)
05-03 20:14:01.596: WARN/System.err(1515):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
05-03 20:14:01.596: WARN/System.err(1515):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1374)
05-03 20:14:01.596: WARN/System.err(1515):     at com.szy.weibo.service.SyncHttp.httpGet(SyncHttp.java:72)
05-03 20:14:01.596: WARN/System.err(1515):     at com.szy.weibo.service.Weibo.getRequestToken(Weibo.java:45)
05-03 20:14:01.606: WARN/System.err(1515):     at com.szy.weibo.MainActivity.startWebView(MainActivity.java:95)
05-03 20:14:01.606: WARN/System.err(1515):     at com.szy.weibo.MainActivity.authorization(MainActivity.java:83)
05-03 20:14:01.606: WARN/System.err(1515):     at com.szy.weibo.MainActivity$1.onClick(MainActivity.java:71)
05-03 20:14:01.606: WARN/System.err(1515):     at android.view.View.performClick(View.java:2408)
05-03 20:14:01.606: WARN/System.err(1515):     at android.view.View$PerformClick.run(View.java:8816)
05-03 20:14:01.616: WARN/System.err(1515):     at android.os.Handler.handleCallback(Handler.java:587)
05-03 20:14:01.627: WARN/System.err(1515):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-03 20:14:01.627: WARN/System.err(1515):     at android.os.Looper.loop(Looper.java:123)
05-03 20:14:01.627: WARN/System.err(1515):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-03 20:14:01.627: WARN/System.err(1515):     at java.lang.reflect.Method.invokeNative(Native Method)
05-03 20:14:01.606: WARN/System.err(1515):     at android.view.View$PerformClick.run(View.java:8816)
05-03 20:14:01.616: WARN/System.err(1515):     at android.os.Handler.handleCallback(Handler.java:587)
05-03 20:14:01.627: WARN/System.err(1515):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-03 20:14:01.627: WARN/System.err(1515):     at android.os.Looper.loop(Looper.java:123)
05-03 20:14:01.627: WARN/System.err(1515):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-03 20:14:01.627: WARN/System.err(1515):     at java.lang.reflect.Method.invokeNative(Native Method)
05-03 20:14:01.627: WARN/System.err(1515):     at java.lang.reflect.Method.invoke(Method.java:521)
05-03 20:14:01.647: WARN/System.err(1515):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-03 20:14:01.647: WARN/System.err(1515):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-03 20:14:01.647: WARN/System.err(1515):     at dalvik.system.NativeStart.main(Native Method)
05-03 20:14:01.606: WARN/System.err(1515):     at android.view.View$PerformClick.run(View.java:8816)
05-03 20:14:01.616: WARN/System.err(1515):     at android.os.Handler.handleCallback(Handler.java:587)
05-03 20:14:01.627: WARN/System.err(1515):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-03 20:14:01.627: WARN/System.err(1515):     at android.os.Looper.loop(Looper.java:123)
05-03 20:14:01.627: WARN/System.err(1515):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-03 20:14:01.627: WARN/System.err(1515):     at java.lang.reflect.Method.invokeNative(Native Method)
05-03 20:14:01.627: WARN/System.err(1515):     at java.lang.reflect.Method.invoke(Method.java:521)
05-03 20:14:01.647: WARN/System.err(1515):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-03 20:14:01.647: WARN/System.err(1515):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-03 20:14:01.647: WARN/System.err(1515):     at dalvik.system.NativeStart.main(Native Method)
05-03 20:14:01.627: WARN/System.err(1515):     at java.lang.reflect.Method.invoke(Method.java:521)
05-03 20:14:01.647: WARN/System.err(1515):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-03 20:14:01.647: WARN/System.err(1515):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-03 20:14:01.647: WARN/System.err(1515):     at dalvik.system.NativeStart.main(Native Method)

回答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 null
        if (responseCode == HTTP_OK)
        {
            inStream = conn.getInputStream();
            response = getResponse(inStream);
        }
        else
        {
            response = "response code:"+responseCode;
        }
    } catch (Exception e)
    {
        // Here you can get the correct response code
        if (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.




回答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)




回答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;
}


来源:https://stackoverflow.com/questions/10431202/java-io-ioexception-received-authentication-challenge-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!