Java 如何配置 HTTPClient 以针对 SOCKS 代理进行身份验证?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1388822/
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-12 11:32:15  来源:igfitidea点击:

How can I configure HTTPClient to authenticate against a SOCKS proxy?

javahttpproxyapache-commons-httpclient

提问by abahgat

I need to set up proxy authentication against a SOCKS proxy. I found out this postgiving instructions that appear to work with common HTTP proxies.

我需要针对 SOCKS 代理设置代理身份验证。我发现这篇文章给出的说明似乎适用于常见的 HTTP 代理。

        httpclient.getHostConfiguration().setProxy("proxyserver.example.com", 8080);

        HttpState state = new HttpState();
        state.setProxyCredentials(new AuthScope("proxyserver.example.com", 8080), 
           new UsernamePasswordCredentials("username", "password"));
        httpclient.setState(state);

Would that work with SOCKSproxies as well or do I have to do something different?

这是否也适用于SOCKS代理,还是我必须做一些不同的事情?

采纳答案by Jesper

The Features pageof Apache HTTPClient says:

Apache HTTPClient的功能页面说:

Transparent connections through SOCKS proxies (version 4 & 5) using native Java socket support.

使用原生 Java 套接字支持通过 SOCKS 代理(版本 4 和 5)的透明连接。

With "transparent", I guess they mean that it works without you needing to do anything special. Do you have a SOCKS proxy available somewhere? Can't you just try it out to see if it works?

对于“透明”,我猜他们的意思是它不需要你做任何特别的事情就可以工作。您在某处有可用的 SOCKS 代理吗?你不能试试看它是否有效吗?

回答by tuergeist

Java supports Socks proxy configuration via preferences:

Java 通过首选项支持 Socks 代理配置:

  • socksProxyHostfor the host name of the SOCKS proxy server
  • socksProxyPortfor the port number, the default value being 1080
  • socksProxyHostSOCKS 代理服务器的主机名
  • socksProxyPort对于端口号,默认值为 1080

e.g.

例如

java -DsocksProxyHost=socks.mydomain.com

(edit) For your example, if the socks proxy was configured in the way outlined before:

(编辑) 对于您的示例,如果以之前概述的方式配置了袜子代理:

httpclient.getHostConfiguration().setProxy("proxyserver.example.com", 8080);
Credentials cred = new UsernamePasswordCredentials("username","password");
httpclient.getState().setProxyCredentials(AuthScope.ANY, cred); 

You can also use this variant (without httpclient):

你也可以使用这个变体(没有 httpclient):

SocketAddress addr = new
InetSocketAddress("webcache.mydomain.com", 8080);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Type.HTTP for HTTP

So completing the previous example, we can now add:

所以完成前面的例子,我们现在可以添加:

URL url = new URL("http://java.sun.com/");
URConnection conn = url.openConnection(proxy);

HTH

HTH

回答by ZZ Coder

SOCKS is not supported by HttpClient 3 natively. You can try the SOCKS support in JDK as suggested by others. The side effect is that your whole JVM will go through the same SOCKS proxy.

HttpClient 3 本身不支持 SOCKS。您可以按照其他人的建议尝试 JDK 中的 SOCKS 支持。副作用是您的整个 JVM 将通过相同的 SOCKS 代理。

Java 5 supports Username/Password authentication in SOCKS (type 2). All you have to do is to setup the authenticator like this,

Java 5 支持 SOCKS 中的用户名/密码认证(类型 2)。你所要做的就是像这样设置验证器,

Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password.toCharArray());
    }
});

Again, this may not work for you because it affects all authentication in your JVM (HTTP auth, Proxy Auth).

同样,这可能对您不起作用,因为它会影响 JVM 中的所有身份验证(HTTP 身份验证、代理身份验证)。

回答by Tomer

You can provide a custom socket factory which implements the SOCKS protocol, and register it as your default HTTP protocol handler. This solution has a limitation similar to tuergeist's answer above has - it applies globally, to any HTTP connection you'll establish through HttpClient.

您可以提供实现 SOCKS 协议的自定义套接字工厂,并将其注册为您的默认 HTTP 协议处理程序。此解决方案具有类似于上面 tuergeist 的答案的限制 - 它在全球范围内适用于您将通过 HttpClient 建立的任何 HTTP 连接。

If you find this a problem, take a look at this correspondence, where Oleg suggests using HttpClient 4.0, but also refers to a possible patch in HostConfiguration class for HttpClient 3.x.

如果您发现这是一个问题,请查看此对应关系,其中 Oleg 建议使用 HttpClient 4.0,但也参考了 HostConfiguration 类中用于 HttpClient 3.x 的可能补丁。

Another possible solution, which is my personal favorite, is to write a wrapper HTTP proxy to the socks proxy.

另一个可能的解决方案,也是我个人最喜欢的,是编写一个包装 HTTP 代理到 socks 代理。

回答by Anshul Chaurasia

I tried

我试过

System.setProperty("socksProxyHost", "socks.xyz.com");
System.setProperty("socksProxyPort", "1000");

and it's working fine.

它工作正常。