HttpClient 4.0.1 - 如何解除连接?
2022-08-31 13:25:43
我在一堆URL上循环,对于每个URL,我都执行以下操作:
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
}
第一个查询很好,第二个查询引发以下异常:
线程“main” java.lang.IllegalStateException 中的异常:单客户端管理无效使用:连接仍已分配。请确保在分配另一个连接之前释放连接。at org.apache.http.impl.conn.SingleClientConnManager.getConnection(SingleClientConnManager.java:199) at org.apache.http.impl.conn.SingleClientConnManager$1.getConnection(SingleClientConnManager.java:173)......
这只是一个简单的单线程应用程序。如何解除此连接?