1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > CloseableHttpClient设置Timeout 设置超时时间

CloseableHttpClient设置Timeout 设置超时时间

时间:2019-03-03 07:21:50

相关推荐

CloseableHttpClient设置Timeout 设置超时时间

进行调用第三方接口的时候 因不清楚发生了什么 所以加个超时时间来避免系统进行长时间停滞

搜索的第一个是

/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);``

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。