ConnectionKeepAliveStrategy kaStrategy = new DefaultConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { long keepAlive = super.getKeepAliveDuration(response, context); if (keepAlive == -1) { //如果服务器没有设置keep-alive这个参数,我们就把它设置成1分钟 keepAlive = 60000; } return keepAlive; } }; CloseableHttpClient httpClient = HttpClients.custom().setKeepAliveStrategy(kaStrategy).build(); HttpGet httpGet = new HttpGet(url); // 设置请求的超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build(); httpGet.setConfig(requestConfig); CloseableHttpResponse response = null; response = httpClient.execute(httpGet);
?
HttpClient 4.3 设置keeplive 和 timeout 的方法
原文:http://supben.iteye.com/blog/2226788