进行调用第三方接口的时候 因不清楚发生了什么 所以加个超时时间来避免系统进行长时间停滞
搜索的第一个是
/chyercn/article/details/97273357?spm=1001..3001.5506
//请求超时 httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); //读取超时 httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,3000);
这个好像已经过时了 所以报错了
/lichmama/p/5715191.html
然后就使用的这个
新建一个RequestConfig:
RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).setStaleConnectionCheckEnabled(true).build();```
这个超时可以设置为客户端级别,作为所有请求的默认值:
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
我这边只使用了上面的 作者下面还有
HttpGet httpget = new HttpGet("/");RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig).setProxy(new HttpHost("myotherproxy", 8080)).build();httpget.setConfig(requestConfig);``