java.io.IOException:ICS 4.0.3中接收到的认证质询为空

我正在尝试从服务器注销。 但是它会返回“0”响应代码和这个异常。 我正在使用GET动词来做到这一点。

logcat的

10-17 14:54:13.261: W/System.err(868): java.io.IOException: Received authentication challenge is null
10-17 14:54:13.284: W/System.err(868):  at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:397)
10-17 14:54:13.284: W/System.err(868):  at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:345)
10-17 14:54:13.304: W/System.err(868):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:276)
10-17 14:54:13.324: W/System.err(868):  at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
10-17 14:54:13.324: W/System.err(868):  at com.remote.synchronizer.haris.CustomHttpClient.executeGet(CustomHttpClient.java:131)
10-17 14:54:13.354: W/System.err(868):  at com.remote.synchronizer.haris.OptionsActivity$1$3$1.run(OptionsActivity.java:87)
10-17 14:54:13.364: W/System.err(868):  at android.os.Handler.handleCallback(Handler.java:605)
10-17 14:54:13.384: W/System.err(868):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 14:54:13.384: W/System.err(868):  at android.os.Looper.loop(Looper.java:137)
10-17 14:54:13.404: W/System.err(868):  at android.app.ActivityThread.main(ActivityThread.java:4424)
10-17 14:54:13.424: W/System.err(868):  at java.lang.reflect.Method.invokeNative(Native Method)
10-17 14:54:13.424: W/System.err(868):  at java.lang.reflect.Method.invoke(Method.java:511)
10-17 14:54:13.454: W/System.err(868):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-17 14:54:13.474: W/System.err(868):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-17 14:54:13.474: W/System.err(868):  at dalvik.system.NativeStart.main(Native Method)
10-17 14:54:13.484: E/HTTP Response(868): java.io.IOException: Received authentication challenge is null

CustomHttpClient.java

public class CustomHttpClient {

    static HttpClient client = new DefaultHttpClient();
    static HttpURLConnection connection = null; 

    public static int executePost(String url, String postParameters)
    {
        int response=0;

        OutputStream output = null;
        try
        {
            connection = (HttpURLConnection)new URL(url).openConnection();
            System.setProperty("http.keepAlive", "false");
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Accept-Charset", "UTF-8");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

            connection.connect();

            output = connection.getOutputStream();
            output.write(postParameters.getBytes("UTF-8"));

            response=connection.getResponseCode();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            Log.e("HTTP Response", e.toString());
        }
        finally {
            if(connection != null) {
                //  connection.disconnect(); 
                if (output != null) 
                    try { output.close(); } 
                catch (IOException logOrIgnore) {}
            }
        }

        return response;
    }

    public static int executeGet(String url)
    {
        int response=0;

        //HttpURLConnection connection = null;
        try
        {
            connection = (HttpURLConnection) new URL(url).openConnection();
            System.setProperty("http.keepAlive", "false");
            //connection.setRequestProperty("Accept-Charset", "UTF-8");
            connection.setDoInput(true);
            connection.setRequestMethod("GET");
            connection.connect();
            response=connection.getResponseCode();
        } 
        catch(Exception e)
        {
            e.printStackTrace();
            Log.e("HTTP Response", e.toString());
        }
        finally {
            if(connection != null) {
                //  connection.disconnect();
            }
        }
        return response;
    }

}

在此之前,我在Gingerbird 2.3中使用DefaultHTTPClient,它的工作完美,但在ICS DefaultHTTPClient中不起作用,所以我需要使用HttpURLConnection。 POST动词工作正常。


如果您在连接对象上再次调用.getResponseCode(),则可以在发生异常后获取响应代码。 这是因为第一次调用.getResponseCode()时,会设置一个内部状态,使得.getResponseCode()可以返回而不会引发异常。

例:

HttpURLConnection connection = ...;
try {
    // Will throw IOException if server responds with 401.
    connection.getResponseCode(); 
} catch (IOException e) {
    // Will return 401, because now connection has the correct internal state.
    int responsecode = connection.getResponseCode(); 
}

我也在这里回答了这个问题:https://stackoverflow.com/a/15972969/816017


HttpURLConnection.getResponseCode()抛出java.io.IOException: Received authentication challenge is null当遇到格式错误的HTTP 401头时, java.io.IOException: Received authentication challenge is null 。 除了HTTP/1.1 401 Unauthorized头之外,您还会从服务器收到WWW-AuthenticateContent-Length头吗? 请参阅IOException:“接收到的认证挑战为空”(Apache Harmony / Android)

如果你不能更改服务器,那么你可以捕获该异常(谢谢https://stackoverflow.com/a/10904318/262462)

try {
    response=connection.getResponseCode();
}
catch (java.io.IOException e) {
    if (e.getMessage().contains("authentication challenge")) {
        response = HttpsURLConnection.HTTP_UNAUTHORIZED;
    } else { throw e; }
}

发生此错误的原因是服务器发送401(未授权),但未给出“WWW-Authenticate”,这是客户提示下一步要做什么。 “WWW-Authenticate”标题告诉客户需要哪种认证(Basic或Digest)。 对于无头HTTP客户端来说,这通常不是很有用,但那就是标准是如何定义的。 发生此错误的原因是lib试图解析“WWW-Authenticate”标题,但不能。

如果可以更改服务器,可能的解决方

  • 添加一个假的“WWW-Authenticate”标头,如: WWW-Authenticate: Basic realm="fake" 。 这仅仅是一种解决方法,不是解决方案,但它应该起作用并且http客户端很满意。
  • 使用HTTP状态码403而不是401 。 它的语义是不一样的,通常当使用登录401是一个正确的答案(详细讨论见这里),但它足够接近。
  • 如果您无法更改服务器,可能的解决方案是:

    正如@ErikZ在他的文章中写道的,你可以使用try&catch

    HttpURLConnection connection = ...;
    try {
        // Will throw IOException if server responds with 401.
        connection.getResponseCode(); 
    } catch (IOException e) {
        // Will return 401, because now connection has the correct internal state.
        int responsecode = connection.getResponseCode(); 
    }
    

    我也在这里发布这个:java.io.IOException:没有发现认证的挑战

    链接地址: http://www.djcxy.com/p/71827.html

    上一篇: java.io.IOException: Received authentication challenge is null in ICS 4.0.3

    下一篇: Return HTTP 403 using Authorize attribute in ASP.Net Core