首页 > Web开发 > 详细

HttpClient 4.3 设置keeplive 和 timeout 的方法

时间:2015-07-15 02:26:20      阅读:10941      评论:0      收藏:0      [点我收藏+]
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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!