android – java.io.IOException:收到的身份验证质询为null

我需要一个响应代码,但它抛出一个IOException.我不知道发生了什么事!

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

IOException是:

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)

解决方法:

通常,您可以通过第二次在连接上调用.getResponseCode()来获取响应代码.起初这对我来说很困惑,但如果你读了HttpURLConnectionImpl足够多次,你的眼睛会止血并看到真相……

如果以这种方式修改代码,您将获得响应代码:

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

请参阅responseCodeAfterException.

上一篇:java – 在Perl中是否有类似于HttpURLConnection的东西?


下一篇:java – 验证CXF HttpAsyncClient使用use.async.http.conduit上下文属性