Java 常见的 HTTP 客户端和代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9811828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:59:37  来源:igfitidea点击:

Common HTTPclient and proxy

javapostproxygethttpclient

提问by dario111cro

I am using apache's common httpclient library. Is it possible to make HTTP request over proxy? More specific, I need to use proxy list for multithreaded POST requests (right now I am testing with single threaded GET requests).

我正在使用 apache 的通用 httpclient 库。是否可以通过代理发出 HTTP 请求?更具体地说,我需要对多线程 POST 请求使用代理列表(现在我正在使用单线程 GET 请求进行测试)。

I tried to use:

我尝试使用:

        httpclient.getHostConfiguration().setProxy("67.177.104.230", 58720);

I get errors with that code:

我收到该代码的错误:

21.03.2012. 20:49:17 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect
21.03.2012. 20:49:17 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
21.03.2012. 20:49:19 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect
21.03.2012. 20:49:19 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
21.03.2012. 20:49:21 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect
21.03.2012. 20:49:21 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
org.apache.commons.httpclient.ProtocolException: The server xxxxx failed to respond with a valid HTTP response
    at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1846)
    at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590)
    at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
    at org.apache.commons.httpclient.ConnectMethod.execute(ConnectMethod.java:144)
    at org.apache.commons.httpclient.HttpMethodDirector.executeConnect(HttpMethodDirector.java:495)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:390)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
    at test.main(test.java:42)

When I remove that line, everything runs fine as expected.

当我删除该行时,一切都按预期运行。

采纳答案by Kai Sternad

For httpclient 4.1.x you can set the proxy like this (taken from this example):

对于 httpclient 4.1.x,您可以像这样设置代理(取自本示例):

    HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        HttpHost target = new HttpHost("issues.apache.org", 443, "https");
        HttpGet req = new HttpGet("/");

        System.out.println("executing request to " + target + " via " + proxy);
        HttpResponse rsp = httpclient.execute(target, req);
        ...
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }

回答by ttati

I had a similar problem with HttpClientversion 4.

我在HttpClient版本 4 中遇到了类似的问题。

I couldn't connect to the server because of a SOCKS proxy error and I fixed it using the below configuration:

由于 SOCKS 代理错误,我无法连接到服务器,我使用以下配置修复了它:

client.getParams().setParameter("socksProxyHost",proxyHost);
client.getParams().setParameter("socksProxyPort",proxyPort);

回答by Dungeon Hunter

Starting from Apache HTTPComponents 4.3.x HttpClientBuilder class sets the proxy defaults from System properties http.proxyHostand http.proxyPortor else you can override them using setProxy method.

Apache的HTTPComponents版本4.3.x HttpClientBuilder类开始设定从系统属性中的代理默认http.proxyHosthttp.proxyPort否则,你可以使用setProxy方法覆盖它们。

回答by Michael Laffargue

Here is how to do that with the last version of HTTPClient(4.3.4)

以下是如何使用HTTPClient(4.3.4)的最新版本执行此操作

    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {
        HttpHost target = new HttpHost("localhost", 443, "https");
        HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");

        RequestConfig config = RequestConfig.custom()
                .setProxy(proxy)
                .build();
        HttpGet request = new HttpGet("/");
        request.setConfig(config);

        System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy);

        CloseableHttpResponse response = httpclient.execute(target, request);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }

回答by Santosh Singh

Although this question is very old, but I see still there are no exact answer. I will try to answer the question here.

虽然这个问题很老了,但我看到仍然没有确切的答案。我将在这里尝试回答这个问题。

I believe the question in short here is how to set the proxy settings for the Apache commons HttpClient (org.apache.commons.httpclient.HttpClient).

我相信这里的问题是如何为 Apache 公共 HttpClient (org.apache.commons.httpclient.HttpClient) 设置代理设置。

Code snippet below should work :

下面的代码片段应该可以工作:

HttpClient client = new HttpClient();
HostConfiguration hostConfiguration = client.getHostConfiguration();
hostConfiguration.setProxy("localhost", 8080);
client.setHostConfiguration(hostConfiguration);

回答by rakensi

Here is how I solved this problem for the old (< 4.3) HttpClient (which I cannot upgrade), using the answer of Santosh Singh (who I gave a +1):

以下是我使用 Santosh Singh(我给了 +1)的回答为旧的(< 4.3)HttpClient(我无法升级)解决这个问题的方法:

HttpClient httpclient = new HttpClient();
if (System.getProperty("http.proxyHost") != null) {
  try {
    HostConfiguration hostConfiguration = httpclient.getHostConfiguration();
    hostConfiguration.setProxy(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort")));
    httpclient.setHostConfiguration(hostConfiguration);
    this.getLogger().warn("USING PROXY: "+httpclient.getHostConfiguration().getProxyHost());
  } catch (Exception e) {
    throw new ProcessingException("Cannot set proxy!", e);
  }
}

回答by Halvor Holsten Strand

If your software uses a ProxySelector(for example for using a PAC-script instead of a static host/port) and your HTTPComponents is version 4.3 or above then you can use your ProxySelectorfor your HttpClientlike this:

如果您的软件使用ProxySelector(例如使用 PAC 脚本而不是静态主机/端口)并且您的 HTTPComponents 是 4.3 或更高版本,那么您可以像这样使用您ProxySelectorHttpClient

ProxySelector myProxySelector = ...;
HttpClient myHttpClient = HttpClientBuilder.create().setRoutePlanner(new SystemDefaultRoutePlanner(myProxySelector))).build();

And then do your requests as usual:

然后像往常一样执行您的请求:

HttpGet myRequest = new HttpGet("/");
myHttpClient.execute(myRequest);