本文介绍了java.io.IOException异常:收到认证挑战是ICS 4.0.3空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从服务器注销。但它返回0响应code与此异常。我使用GET动词来做到这一点。

I'm trying to logoff from the server. But it returns "0" response code with this exception. I'm using GET verb to do this.

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谓词是工作的罚款。

Before this I'm using DefaultHTTPClient in Gingerbird 2.3 its working perfectly but in ICS DefaultHTTPClient is not working so I need to use HttpURLConnection. POST verb is working fine.

推荐答案

您可以得到一个异常后的反应code,如果你打电话.getResponse code()第二次连接对象上。这是因为你第一次调用.getResponse code()的内部状态设置,使.getResponse code(),以未抛出异常返回。

You can get the response code after an exception if you call .getResponseCode() a second time on the connection object. This is because the first time you call .getResponseCode() an internal state is set that enables .getResponseCode() to return without throwing an exception.

例如:

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

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

这篇关于java.io.IOException异常:收到认证挑战是ICS 4.0.3空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 05:23