如何配置 HTTPClient 以针对 SOCKS 代理进行身份验证?
我需要针对 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);
这也适用于 SOCKS 代理吗?还是我必须做一些不同的事情?
I need to set up proxy authentication against a SOCKS proxy.
I found out this post giving instructions that appear to work with common HTTP proxies.
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 SOCKS proxies as well or do I have to do something different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Java 通过首选项支持 Socks 代理配置:
socksProxyHost
为 SOCKS 代理服务器的主机名socksProxyPort
为端口号,默认值为1080
例如
(编辑)对于您的示例,如果以前面概述的方式配置socks代理:
您也可以使用此变体(没有httpclient):
因此完成前面的示例,我们现在可以添加:
HTH
Java supports Socks proxy configuration via preferences:
socksProxyHost
for the host name of the SOCKS proxy serversocksProxyPort
for the port number, the default value being1080
e.g.
(edit) For your example, if the socks proxy was configured in the way outlined before:
You can also use this variant (without httpclient):
So completing the previous example, we can now add:
HTH
Apache HTTPClient 的 功能页面 说:
对于“透明”,我猜他们的意思是它不需要你做任何特别的事情就可以工作。你有可用的 SOCKS 代理吗?你就不能尝试一下看看是否有效吗?
The Features page of Apache HTTPClient says:
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?
HttpClient 3 本身不支持 SOCKS。您可以按照其他人的建议尝试 JDK 中的 SOCKS 支持。副作用是您的整个 JVM 将通过相同的 SOCKS 代理。
Java 5 支持 SOCKS 中的用户名/密码身份验证(类型 2)。您所要做的就是像这样设置身份验证器,
同样,这可能对您不起作用,因为它会影响 JVM 中的所有身份验证(HTTP 身份验证、代理身份验证)。
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.
Java 5 supports Username/Password authentication in SOCKS (type 2). All you have to do is to setup the authenticator like this,
Again, this may not work for you because it affects all authentication in your JVM (HTTP auth, Proxy Auth).
您可以提供一个实现 SOCKS 协议的自定义套接字工厂,并将其注册为默认的 HTTP 协议处理程序。该解决方案有一个类似于上面 tuergeist 的答案的限制 - 它全局适用于您将通过 HttpClient 建立的任何 HTTP 连接。
如果您发现此问题,请查看 此对应关系,其中 Oleg 建议使用 HttpClient 4.0,但也提到了 HttpClient 3.x 的 HostConfiguration 类中可能的补丁。
另一种可能的解决方案,也是我个人最喜欢的,是为socks代理编写一个包装HTTP代理。
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.
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.
Another possible solution, which is my personal favorite, is to write a wrapper HTTP proxy to the socks proxy.
我尝试过
并且工作正常。
I tried
and it's working fine.