Ric*_*d H 70 java connection apache-httpclient-4.x
我对一堆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)......
这只是一个简单的单线程应用程序.我该如何发布此连接?
Yad*_*adu 95
Httpcomponents 4.1推荐的方法是关闭连接并释放任何底层资源:
EntityUtils.consume(HttpEntity)
Run Code Online (Sandbox Code Playgroud)
其中HttpEntity通过是一个响应实体.
Sni*_*las 32
这似乎很有效:
if( response.getEntity() != null ) {
response.getEntity().consumeContent();
}//if
Run Code Online (Sandbox Code Playgroud)
即使您没有打开其内容,也不要忘记使用该实体.例如,您希望响应中的HTTP_OK状态并且没有得到它,您仍然必须使用该实体!
Ric*_*d H 23
要回答我自己的问题:要释放连接(以及与请求关联的任何其他资源),您必须关闭HttpEntity返回的InputStream:
InputStream is = entity.getContent();
.... process the input stream ....
is.close(); // releases all resources
Run Code Online (Sandbox Code Playgroud)
来自文档
Chr*_*ris 12
我正在讨论专门针对Apache HttpClient 4.0.1的详细解决方案.我正在使用这个HttpClient版本,因为它是由WAS v8.0提供的,我需要在Apache Wink v1.1.1中使用提供的HttpClient,也是由WAS v8.0提供的,对Sharepoint进行一些NTLM认证的REST调用.
在Apache HttpClient邮件列表上引用Oleg Kalnichevski:
几乎所有这些代码都不是必需的.(1)只要实体内容被消耗到流的末尾,HttpClient就会自动释放底层连接; (2)HttpClient将在读取响应内容时自动释放引发的任何I/O异常的底层连接.在这种情况下不需要特殊处理.
事实上,这足以确保正确释放资源:
Run Code Online (Sandbox Code Playgroud)HttpResponse rsp = httpclient.execute(target, req); HttpEntity entity = rsp.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); try { // process content } finally { instream.close(); // entity.consumeContent() would also do } }这就对了.
如果不消耗响应,则可以使用以下代码中止请求:
// Low level resources should be released before initiating a new request
HttpEntity entity = response.getEntity();
if (entity != null) {
// Do not need the rest
httpPost.abort();
}
Run Code Online (Sandbox Code Playgroud)
参考:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e143
Apache HttpClient版本:4.1.3
| 归档时间: |
|
| 查看次数: |
134771 次 |
| 最近记录: |