HttpClient4.X Invalid use of SingleClientConnManager: connection still allocated问题

HttpClient4.X Invalid use of SingleClientConnManager: connection still allocated问题

这次的项目首次大量用到了httpclient4.1开发。但仅仅在测试环境就发生了无法连接的情况,异常报

Invalid use of SingleClientConnManager: connection still allocated

从国外的一张帖子看到,httpclient默认使用的是SingleClientConnManager,但在并发环境下最好使用ThreadSafeClientConnManager。代码如下:

HttpClient client = new DefaultHttpClient(new ThreadSafeClientConnManager());

HttpGet httpGet = null;

InputStream in = null;

try {

httpGet = new HttpGet(url);

HttpResponse response = client.execute(httpGet);

HttpEntity responseEntity = response.getEntity();

if (responseEntity != null) {

in = response.getEntity().getContent();

return EntityUtils.toByteArray(responseEntity);

}

} finally {

if (httpGet != null) {

httpGet.abort();

}

IOUtils.closeQuietly(in);

}

return null;

这样就可以避免发生异常,感谢国外的这个帖子

http://old.nabble.com/HttpClient-instance-management-td24185137.html


http超时处理:

使用是apache的HttpClient:

DefaultHttpClient:
请求超时
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
读取超时
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);

HttpClient
HttpClient httpClient=new HttpClient();
链接超时
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000); 
读取超时
httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000)


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值