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超时处理:

使用是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)

发布评论

分享到:

IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Invalid use of SingleClientConnManager: connection still allocated. 解决方法讲解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。