创建httpClient时,设置timeout时间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建httpClient时,设置timeout时间相关的知识,希望对你有一定的参考价值。
//private static HttpClient httpClient = HttpClientBuilder.create().build();
private static CloseableHttpClient httpClient;// = HttpClientBuilder.create().build(); //no timeout handling...
static {
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
//客户端和服务器建立连接的timeout
requestConfigBuilder.setConnectTimeout(30000);
//从连接池获取连接的timeout
requestConfigBuilder.setConnectionRequestTimeout(30000);
//连接建立后,request没有回应的timeout
requestConfigBuilder.setSocketTimeout(30000);
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
clientBuilder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(30000).build()); //连接建立后,request没有回应的timeout
clientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
httpClient = clientBuilder.build();
以上是关于创建httpClient时,设置timeout时间的主要内容,如果未能解决你的问题,请参考以下文章