java DefaultHttpClient 超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16088378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:52:26  来源:igfitidea点击:

Timeout in DefaultHttpClient

javaandroidtimeouthttpclient

提问by Xander

I'm a little confused on how the timeouts in DefaultHttpClient work.

我对 DefaultHttpClient 中的超时如何工作感到有些困惑。

I'm using this code:

我正在使用此代码:

private DefaultHttpClient createHttpClient() {
        HttpParams my_httpParams = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(my_httpParams, 3000);
        HttpConnectionParams.setSoTimeout(my_httpParams, 15000);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        ThreadSafeClientConnManager multiThreadedConnectionManager = new ThreadSafeClientConnManager(my_httpParams, registry);

        DefaultHttpClient httpclient = new DefaultHttpClient(multiThreadedConnectionManager, my_httpParams);

        return httpclient;
}

.

.

String url = "http://www.example.com";

DefaultHttpClient httpclient = createHttpClient();
HttpGet httpget = new HttpGet(url);

try {
    HttpResponse response = httpclient.execute(httpget);
    StatusLine statusLine = response.getStatusLine();
    mStatusCode = statusLine.getStatusCode();

    if (mStatusCode == 200){
        content = EntityUtils.toString(response.getEntity());
    }

} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (IllegalStateException e){
    e.printStackTrace();
}

When 15 seconds have passed and not all data has been received, an exception will be thrown, right? But on which method? I thought it to be the .execute(httpget)method but that one only tells me it throws ClientProtocolExceptionand IOException. Could anyone help me clearifying this?

当 15 秒过去了还没有收到所有数据时,会抛出异常,对吗?但是用哪种方法?我认为这是.execute(httpget)方法,但那个人只告诉我它抛出ClientProtocolExceptionIOException。有人可以帮我澄清一下吗?

回答by Deepak Bala

It does throw an exception on execute(). The parent of SocketTimeoutExceptionis an IOException. A catch block handling IOExceptionwill be able to catch both.

它确实在 上抛出异常execute()。的父级SocketTimeoutExceptionIOException. catch 块处理IOException将能够同时捕获两者。

Try executing this code.

尝试执行此代码。

HttpParams my_httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(my_httpParams, 3000);
HttpConnectionParams.setSoTimeout(my_httpParams, 1);
DefaultHttpClient defaultHttpClient = new DefaultHttpClient(my_httpParams);
HttpGet httpGet = new HttpGet("http://google.com");
defaultHttpClient.execute(httpGet);

It results in this exception.

它导致此异常。

java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(Unknown Source)
    ...
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)

You can always choose to selectively process the exception by catching it and handling IOExceptionlater.

您始终可以选择通过捕获并IOException稍后处理来选择性地处理异常。

try
{
    // Your code
}
catch (SocketTimeoutException e)
{
    // handle timeouts
    e.printStackTrace();
}
catch (IOException e)
{
    // handle other IO exceptions
    e.printStackTrace();
}

回答by Tim Perry

If you look at the Apache docs at http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e249, it notes that connection timeout exceptions are IOException subclasses.

如果您查看http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e249上的 Apache 文档,它会指出连接超时异常是 IOException 子类。

To be more specific, I believe they'll either ConnectTimeoutExceptionsif the connection can't be set up within your configured connection timeout, or SocketTimeoutExceptions, if it's set up but no data is received for your configured SO timeout.

更具体地说,我相信如果在您配置的连接超时内无法建立连接,他们将是ConnectTimeoutExceptions,或者是SocketTimeoutExceptions,如果它已设置但未收到您配置的 SO 超时的数据。