相关疑难解决方法(0)

HttpClient 4.0.1 - 如何释放连接?

我对一堆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

}
Run Code Online (Sandbox Code Playgroud)

第一个查询没问题,第二个查询抛出此异常:

线程"main"中的异常java.lang.IllegalStateException:无效使用SingleClientConnManager:仍然分配了连接.确保在分配另一个连接之前释放连接.在org.apache.http.impl.conn.SingleClientConnManager.getConnection(SingleClientConnManager.java:199)org.apache.http.impl.conn.SingleClientConnManager $ 1.getConnection(SingleClientConnManager.java:173)......

这只是一个简单的单线程应用程序.我该如何发布此连接?

java connection apache-httpclient-4.x

70
推荐指数
6
解决办法
13万
查看次数

标签 统计

apache-httpclient-4.x ×1

connection ×1

java ×1