昨天遇到一个问题需要设置CloseableHttpClient的超时时间,查了官方文档如下。
新建一个RequestConfig:
RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).setStaleConnectionCheckEnabled(true).build();
这个超时可以设置为客户端级别,作为所有请求的默认值:
CloseableHttpClient httpclient = HttpClients.custom() ???.setDefaultRequestConfig(defaultRequestConfig) ???.build();
Request不会继承客户端级别的请求配置,所以在自定义Request的时候,需要将客户端的默认配置拷贝过去:
HttpGet httpget = new HttpGet("http://www.apache.org/");RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig) ???.setProxy(new HttpHost("myotherproxy", 8080)) ???.build();httpget.setConfig(requestConfig);
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
CloseableHttpClient设置Timeout
原文地址:https://www.cnblogs.com/skinchqqhah/p/10351447.html