Java : HttpClient 4.1.2 : ConnectionTimeout、SocketTimeout 值设置无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9725492/
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
Java : HttpClient 4.1.2 : ConnectionTimeout, SocketTimeout values set are not effective
提问by Shadab Khan
I am using HttpClient 4.1.2. Setting ConnectionTimeout and SocketTimeout to a value is never effective.
我正在使用 HttpClient 4.1.2。将 ConnectionTimeout 和 SocketTimeout 设置为一个值永远不会有效。
code :
代码 :
Long startTime = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 30);
HttpConnectionParams.setSoTimeout(params, 60);
HttpGet httpget = new HttpGet("http://localhost:8080/Test/ScteServer");
try {
startTime = System.currentTimeMillis();
HttpResponse response = httpClient.execute(httpget);
}
catch(SocketTimeoutException se) {
Long endTime = System.currentTimeMillis();
System.out.println("SocketTimeoutException :: time elapsed :: " + (endTime-startTime));
se.printStackTrace();
}
catch(ConnectTimeoutException cte) {
Long endTime = System.currentTimeMillis();
System.out.println("ConnectTimeoutException :: time elapsed :: " + (endTime-startTime));
cte.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
Long endTime = System.currentTimeMillis();
System.out.println("IOException :: time elapsed :: " + (endTime-startTime) );
e.printStackTrace();
}
If the server is down, then the connection timeout is never before 400 ms when it has to timeout at ~ 30 ms as configured.
如果服务器关闭,则连接超时绝不会超过 400 毫秒,而必须按照配置在 ~ 30 毫秒时超时。
Same is the case for Socket Timeout, putting a sleep in doGet() for 5000 ms will throw a socket timeout which will never be at around 60 ms as configured. It takes more than 500 ms.
套接字超时的情况也是如此,在 doGet() 中休眠 5000 毫秒将引发套接字超时,该超时永远不会达到配置的 60 毫秒左右。它需要超过 500 毫秒。
Can anyone suggest how to configure HttpClient 4.1.2 so that it times out around the configured time?
任何人都可以建议如何配置 HttpClient 4.1.2 以便它在配置的时间内超时?
回答by Vlad
The HttpConnectionParams
need to be passed to a connection manager (see this question). When using the DefaultHttpClient
you can set these parameters like this:
将HttpConnectionParams
需要被传递给连接管理器(见这个问题)。使用时,DefaultHttpClient
您可以像这样设置这些参数:
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
See the documentationalso!
另请参阅文档!
回答by Evgenii Bogdanov
You can try this (works with apache http-client 4.5.2):
你可以试试这个(适用于 apache http-client 4.5.2):
int DEFAULT_TIMEOUT = 5000;
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(DEFAULT_TIMEOUT)
.setConnectionRequestTimeout(DEFAULT_TIMEOUT)
.setSocketTimeout(DEFAULT_TIMEOUT)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();