HttpClient 4.5版本設定連線超時時間-CloseableHttpClient設定Timeout(區別於4.3.2)
HttpClient升級到4.5版本後,API有很多變化,HttpClient 4之後,API一直沒有太穩定,我感覺4.5版本抽象後,很多API應該快穩定了。
使用HttpClient,一般都需要設定連線超時時間和獲取資料超時時間。這兩個引數很重要,目的是為了防止訪問其他http時,由於超時導致自己的應用受影響。
4.5版本中,這兩個引數的設定都抽象到了RequestConfig中,由相應的Builder構建,具體的例子如下:
- <code class="hljs go" style="font-family:'Courier New',Courier,monospace; font-size:1em">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("得到的結果:" + response.getStatusLine());//得到請求結果
- HttpEntity entity = response.getEntity();//得到請求回來的資料</code>
setConnectTimeout:設定連線超時時間,單位毫秒。
setConnectionRequestTimeout:設定從connect Manager獲取Connection 超時時間,單位毫秒。這個屬性是新加的屬性,因為目前版本是可以共享連線池的。
setSocketTimeout:請求獲取資料的超時時間,單位毫秒。 如果訪問一個介面,多少時間內無法返回資料,就直接放棄此次呼叫。
===========================================
昨天遇到一個問題需要設定CloseableHttpClient的超時時間,查了官方文件如下。
新建一個RequestConfig:
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setStaleConnectionCheckEnabled(true)
.build();
這個超時可以設定為客戶端級別,作為所有請求的預設值:
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultRequestConfig(defaultRequestConfig)
.build();
Request不會繼承客戶端級別的請求配置,所以在自定義Request的時候,需要將客戶端的預設配置拷貝過去:
HttpGet httpget = new HttpGet("http://www.apache.org/");
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
.setProxy(new HttpHost("myotherproxy", 8080))
.build();
httpget.setConfig(requestConfig);
倫理片 http://www.dotdy.com/
4.3版本的超時是這樣的:
public static String httpPost(String url, String jsonString) { // 設定HTTP請求引數 String result = null; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); try { httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);//設定請求超時時間 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介面呼叫異常:url is::" + url, e); return null; } finally { httpClient.getConnectionManager().shutdown(); } return result; }
4.5.2版本是這樣的:
public static String testTimeout(String url) {
// 設定HTTP請求引數
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介面呼叫異常:url is::" + url, e);
return null;
} catch (Exception e) {
logger.error("http介面呼叫異常:url is::" + url, e);
return null;
} finally {
try {
client.close();
} catch (IOException e) {
logger.error("http介面呼叫異常:url is::" + url, e);
}
}
return result;
}
轉自:http://blog.csdn.net/h254532699/article/details/54342470