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

发布于 2024-08-04 05:54:45 字数 577 浏览 6 评论 0原文

我需要针对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆依然 2024-08-11 05:54:45

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

  • socksProxyHost 为 SOCKS 代理服务器的主机名
  • socksProxyPort 为端口号,默认值为 1080

例如

java -DsocksProxyHost=socks.mydomain.com

编辑)对于您的示例,如果以前面概述的方式配置socks代理:

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

您也可以使用此变体(没有httpclient):

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

因此完成前面的示例,我们现在可以添加:

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

HTH

Java supports Socks proxy configuration via preferences:

  • socksProxyHost for the host name of the SOCKS proxy server
  • socksProxyPort for the port number, the default value being 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):

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

纸短情长 2024-08-11 05:54:45

Apache HTTPClient 的 功能页面 说:

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

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

The Features page of Apache HTTPClient says:

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

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?

Bonjour°[大白 2024-08-11 05:54:45

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

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

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

同样,这可能对您不起作用,因为它会影响 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,

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).

穿越时光隧道 2024-08-11 05:54:45

您可以提供一个实现 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.

淡淡绿茶香 2024-08-11 05:54:45

我尝试过

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

并且工作正常。

I tried

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

and it's working fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文