如何在httpcomponents httpclient中设置连接超时?我在http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html上找到了文档,但尚不清楚如何实际设置这些参数。
另外,解释SO_TIMEOUT
和CONNECTION_TIMEOUT
之间的区别将很有帮助。
最佳答案
在Apache Http Client的4.3版中,该配置被重构(再次)。新方法如下所示:
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setConnectTimeout(timeout);
requestBuilder.setConnectionRequestTimeout(timeout);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultRequestConfig(requestBuilder.build());
HttpClient client = builder.build();