3

我需要一个响应代码,但它会抛出一个 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)
4

3 回答 3

6

大多数情况下,您可以通过在连接上再次调用 .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。

于 2013-04-12T13:44:03.217 回答
3

如果您可以对服务器端进行更改,则可以确保您发送了带有 401 状态代码的正确标头。来自http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2

10.4.2 401 未经授权

该请求需要用户身份验证。响应必须包含一个 WWW-Authenticate 头域(第 14.47 节)包含适用于所请求资源的质询。客户端可以使用合适的授权头域重复请求(第 14.8 节)。如果请求已包含授权凭证,则 401 响应表示已拒绝对这些凭证的授权。如果 401 响应包含与先前响应相同的质询,并且用户代理已经尝试了至少一次身份验证,则应该向用户呈现响应中给出的实体,因为该实体可能包含相关的诊断信息。HTTP 访问身份验证在“HTTP 身份验证:基本和摘要式访问身份验证”[43] 中进行了说明。

我使用与@tj-thind 相同的代码,直到遇到@Chiara 描述的情况。然后我在我的服务器脚本中添加了WWW-AuthenticateContent-Length头,现在我可以在使用getResponseCode().

另请参阅IOException:“收到的身份验证质询为空”(Apache Harmony/Android)

于 2012-11-09T12:09:53.833 回答
0

我自己以前也遇到过这个问题。该异常实际上是getResponseCode在 401 事件中引发的,这是未经授权的 HTTP 代码。这似乎是 Android 实现所独有的HttpURLConnection。我发现处理它的最简单方法就是处理 catch 块中的 401。

catch (java.io.IOException e) {
    //Probably got a 401 here
    response = "response code:"+401;
}
于 2012-06-05T20:19:43.837 回答