1. //private static HttpClient httpClient = HttpClientBuilder.create().build();  

  2. private static CloseableHttpClient httpClient;// = HttpClientBuilder.create().build(); //no timeout handling...  

  3.   

  4. static {  

  5.    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();  

  6.    //客户端和服务器建立连接的timeout  

  7.    requestConfigBuilder.setConnectTimeout(30000);  

  8.    //从连接池获取连接的timeout  

  9.    requestConfigBuilder.setConnectionRequestTimeout(30000);  

  10.    //连接建立后,request没有回应的timeout  

  11.    requestConfigBuilder.setSocketTimeout(30000);  

  12.   

  13.    HttpClientBuilder clientBuilder = HttpClientBuilder.create();  

  14.    clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());  

  15.    clientBuilder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(30000).build()); //连接建立后,request没有回应的timeout  

  16.    clientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());  

  17.    httpClient = clientBuilder.build();