【问题标题】:Apache Http Client slower than browser downloadApache Http Client 比浏览器下载慢
【发布时间】:2018-01-11 21:43:07
【问题描述】:

我正在从 github 下载一个大小为 300M 的大型存储库。从浏览器下载需要 10-15 秒。在同一台机器上,我使用下面的代码下载需要 110-120 秒。我想知道我是否做错了。请建议我使用 apache http 客户端获得相同的速度(10-15 秒)。或者有什么比http客户端更好的吗?

Apache httpclient = 4.5

java - 8

我使用的代码:

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class Downloader {

    public File download(URL url, File dstFile) {
        PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
        manager.setDefaultMaxPerRoute(20);
        manager.setMaxTotal(200);
        CloseableHttpClient httpclient = HttpClientBuilder.create()
                .setConnectionManager(manager)
                .build();

//  Second option: it also takes same time.
//        .setRedirectStrategy(new LaxRedirectStrategy())
//        .setMaxConnTotal(2 * 50)
//        .setMaxConnPerRoute(50)
//        .build();
//      CloseableHttpClient httpclient = HttpClients.custom()
//              .setRedirectStrategy(new LaxRedirectStrategy()) // adds HTTP REDIRECT support to GET and POST methods 
//              .build();
        try {
            HttpGet get = new HttpGet(url.toURI()); // we're using GET but it could be via POST as well
            File downloaded = httpclient.execute(get, new FileDownloadResponseHandler(dstFile));
            return downloaded;
        } catch (Exception e) {
            throw new IllegalStateException(e);
        } finally {
            IOUtils.closeQuietly(httpclient);
        }
    }

    static class FileDownloadResponseHandler implements ResponseHandler<File> {

        private final File target;

        public FileDownloadResponseHandler(File target) {
            this.target = target;
        }

        @Override
        public File handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            InputStream source = response.getEntity().getContent();
            FileUtils.copyInputStreamToFile(source, this.target);
            return this.target;
        }

    }

}

【问题讨论】:

  • 我看不出你的代码有什么本质上的错误。我不确定chrome是否使用http协议来下载文件或其他东西。这可以解释差异。
  • 是的。我尝试使用所有可能的选项,例如 PooledHttpClientManager、ThreadSafe 客户端管理器。我没有运气。你对多线程下载有什么想法吗,比如 IDM(互联网下载管理器),用于下载音频、视频。
  • 谢谢。我会试一试。
  • 它没有用。相同的速度(120 秒)。 :(

标签: download apache-httpclient-4.x


【解决方案1】:

我遇到了同样的问题;修复是从这里执行建议:https://stackoverflow.com/a/35458078/9204142,即在RequestConfig 级别使用disableContentCompression()contentCompressionEnabled=false。 修复后速度与 curl 匹配。

由于我使用的是 Apache Camel,因此修复意味着在 Endpoint 的 URI 中添加 ?httpClient.contentCompressionEnabled=false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-08
    • 2015-04-25
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多