[英]Httpclient take a very long time
I'm trying to download many files using the Apache HttpComponents library
. 我正在尝试使用Apache HttpComponents library
下载许多文件。
Files are downloaded separately using the same HttpClient. 使用相同的HttpClient分别下载文件。
But I get only the first one. 但是我只有第一个。 When I use the debugger, the program stay blocked on this instruction 当我使用调试器时,程序在此指令下保持阻塞状态
HttpResponse response = httpClient.execute(httpGet);
I'm dowloading files on seperate threads using the ExecutorService : 我正在使用ExecutorService在不同的线程上下载文件:
ExecutorService pool = Executors.newSingleThreadExecutor(); pool.submit(new FileDownloader(httpClient,URL1)); pool.submit(new FileDownloader(httpClient,URL2)); pool.shutdown(); pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
EDIT 编辑
It works when I use a seperate httpClient. 当我使用单独的httpClient时,它可以工作。
Any idea ? 任何想法 ?
The information you've given is rather sparse. 您提供的信息很少。 I'm guessing that you're using the Apache HttpComponents library . 我猜您正在使用Apache HttpComponents库 。 In that case, when using an HttpClient
from more than one thread, you will need to use a thread-safe connection pool manager. 在这种情况下,当从多个线程中使用HttpClient
时,您将需要使用线程安全的连接池管理器。
For instance: PoolingHttpClientConnectionManager
例如: PoolingHttpClientConnectionManager
See this tutorial for an example. 有关示例,请参见本教程 。
It was a problem due to the proxy of my company. 由于我公司的代理,这是一个问题。 It doesn't allow more than 2 openened connection in the same time. 不允许同时打开两个以上的连接。 So to avoid this problem, don't forget to close the HttpResponse after every request. 因此,为避免此问题,请不要忘记在每个请求之后关闭HttpResponse。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.