本文介绍了HttpClient 4.0.1 - 如何释放连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个循环的一堆URL,对于每一个我正在做以下:
I have a loop over a bunch of URLs, for each one I'm doing the following:
private String doQuery(String url) {
HttpGet httpGet = new HttpGet(url);
setDefaultHeaders(httpGet); // static method
HttpResponse response = httpClient.execute(httpGet); // httpClient instantiated in constructor
int rc = response.getStatusLine().getStatusCode();
if (rc != 200) {
// some stuff...
return;
}
HttpEntity entity = response.getEntity();
if (entity == null) {
// some stuff...
return;
}
// process the entity, get input stream etc
}
第一个查询是正常的,第二个查询会抛出此异常:
The first query is fine, the second throws this exception:
这只是一个简单的单线程应用程序。如何释放此连接?
This is just a simple single-threaded app. How can I release this connection?
推荐答案
回答我自己的问题:释放连接请求)您必须关闭由HttpEntity返回的InputStream:
To answer my own question: to release the connection (and any other resources associated with the request) you must close the InputStream returned by the HttpEntity:
InputStream is = entity.getContent();
.... process the input stream ....
is.close(); // releases all resources
从
这篇关于HttpClient 4.0.1 - 如何释放连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!