本文主要是介绍HttpClient4.X 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超时处理:
使用是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)
这篇关于HttpClient4.X Invalid use of SingleClientConnManager: connection still allocated解决方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!