说明: 项目中使用的HttpClient版本是3.1
测试
一般的HttpClient使用例子:
MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
HttpClient client = new HttpClient(manager);
client.setConnectionTimeout(30000);
client.setTimeout(30000);
GetMethod get = new GetMethod("http://download.jboss.org/jbossas/7.0/jboss-7.0.0.Alpha1/jboss-7.0.0.Alpha1.zip");
try {
client.executeMethod(get); //发起请求
String result = get.getResponseBodyAsString(); //获取数据
} catch (Exception e) {
} finally {
get.releaseConnection(); //释放链接
}
这里一个url是近20MB的一个下载资源,很快发现线程要等个很久。得加个timeout超时机制。
分析
目前httpClient3.1只支持3种timeout的设置:
connectionTimeout : socket建立链接的超时时间,Httpclient包中通过一个异步线程去创建socket链接,对应的超时控制。
timeoutInMilliseco