SimpleHttpConnectionManager使用不正确

MAr*_*rio 10 java http apache-commons

SimpleHttpConnectionManager使用不正确.确保始终调用HttpMethod.releaseConnection(),并且一次只有一个线程和/或方法使用此连接管理器.

有谁知道为什么会出现此错误并导致我要下载的文件或失败并重试或下载未完成

谢谢 !

Eya*_*der 14

确保不使用SimpleHttpConnectionManager来创建和使用来自多个线程的连接.简单的连接管理器不是为它设计的 - 它总是返回相同的连接,这不是线程安全的.

在多线程环境中,使用使用连接池的其他管理器.请参见MultiThreadedHttpConnectionManager.


wmo*_*365 8

我不愿意这样做,但根据Eyal Schneider的回答,在Vincent de Villers优秀博客中找到有关使用MultiThreadedHttpConnectionManager的更多信息.

如果链接消失,则复制代码段:

HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager());
GetMethod httpget = new GetMethod("http://www.myhost.com/");
try {
    httpclient.executeMethod(httpget);
    Reader reader = new InputStreamReader(
        httpget.getResponseBodyAsStream(), httpget.getResponseCharSet());
    // consume the response entity
} finally {
    httpget.releaseConnection();
}
Run Code Online (Sandbox Code Playgroud)