Apache HttpComponents HttpClient超时 [英] Apache HttpComponents HttpClient timeout
问题描述
如何在httpcomponents httpclient中设置连接超时?我在以下网址找到了文档: http://hc.apache。 org / httpcomponents-client-ga / tutorial / html / connmgmt.html 但不清楚这些参数是如何实际设置的。
How do I set the connection timeout in httpcomponents httpclient? I have found the documentation at: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html but it is not clear how these parameters are actually set.
另外,一个解释 SO_TIMEOUT
和 CONNECTION_TIMEOUT
之间的差异会有所帮助。
Also, an explanation of the difference between SO_TIMEOUT
and CONNECTION_TIMEOUT
would be helpful.
推荐答案
在Apache Http Client的4.3版中,配置被重构(再次)。新方法如下所示:
In version 4.3 of Apache Http Client the configuration was refactored (again). The new way looks like this:
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder = requestBuilder.setConnectTimeout(timeout);
requestBuilder = requestBuilder.setConnectionRequestTimeout(timeout);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultRequestConfig(requestBuilder.build());
HttpClient client = builder.build();
这篇关于Apache HttpComponents HttpClient超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!