HttpClient4.X Invalid use of SingleClientConnManager: connection still allocated

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

 

The project uses httpclient4.1 development. In the case of multi-threading, the exception is reported

 

Invalid use of SingleClientConnManager: connection still allocated

 

From a foreign post, httpclient uses SingleClientConnManager by default, but it is best to use ThreadSafeClientConnManager in a concurrent environment. code show as below:

 

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;

 

In this way, exceptions can be avoided, thank you for this post abroad

 

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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326358353&siteId=291194637