HttpClient 4.5 version sets the connection timeout - CloseableHttpClient sets Timeout (different from 4.3.2)
After HttpClient is upgraded to version 4.5, there are many changes to the API. After HttpClient 4, the API has not been too stable. I feel that after the 4.5 version is abstracted, many APIs should be stable.
With HttpClient, you usually need to set the connection timeout and get the data timeout. These two parameters are important in order to prevent your application from being affected due to timeouts when accessing other https.
In version 4.5, the settings of these two parameters are abstracted into RequestConfig and built by the corresponding Builder. The specific examples are as follows:
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://stackoverflow.com/");
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000).setConnectionRequestTimeout(1000)
.setSocketTimeout(5000).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = httpclient.execute(httpGet);
System.out.println("Get the result:" + response.getStatusLine());//Get the requested result
HttpEntity entity = response.getEntity();//Get the data back from the request
setConnectTimeout: Sets the connection timeout in milliseconds.
setConnectionRequestTimeout: Sets the connection timeout time in milliseconds from the connect manager. This property is a new property because the current version is a shared pool.
setSocketTimeout: Timeout in milliseconds for requesting data. If you access an interface and can't return data for a while, just abandon the call.
===========================================
Yesterday encountered a problem need to set the timeout time of CloseableHttpClient, check the official documents are as follows.
Create a new RequestConfig:
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setStaleConnectionCheckEnabled(true)
.build();
This timeout can be set to the client level as the default for all requests:
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultRequestConfig(defaultRequestConfig)
.build();
Request does not inherit the client-level request configuration, so when customizing the Request, you need to copy the client's default configuration:
HttpGet httpget = new HttpGet("http://www.apache.org/");
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
.setProxy(new HttpHost("myotherproxy", 8080))
.build();
httpget.setConfig(requestConfig);
The timeout for version 4.3 is like this:
public static String httpPost(String url, String jsonString) { / / Set the HTTP request parameters String result = null; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); try { httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);/ / Set the request timeout time 10s StringEntity entity = new StringEntity(jsonString); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpEntity resEntity = httpClient.execute(httpPost).getEntity(); result = EntityUtils.toString(resEntity, "UTF-8"); } catch (Exception e) { logger.error("http interface call exception: url is::" + url, e); return null; } finally { httpClient.getConnectionManager().shutdown(); } return result; }
The 4.5.2 version is like this:
public static String testTimeout(String url) {
/ / Set the HTTP request parameters
String result = null;
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(50000).setConnectionRequestTimeout(10000)
.setSocketTimeout(50000).build();
httpGet.setConfig(requestConfig);
try {
CloseableHttpResponse response = client.execute(httpGet);
result = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (ClientProtocolException e) {
Logger.error("http interface call exception: url is::" + url, e);
return null;
} catch (Exception e) {
Logger.error("http interface call exception: url is::" + url, e);
return null;
} finally {
try {
client.close();
} catch (IOException e) {
Logger.error("http interface call exception: url is::" + url, e);
}
}
return result;
}