支持身份验证的 Java Proxy 客户端类
我正在寻找一个支持身份验证的 javaocks 代理客户端类,有什么建议吗? java.net.Proxy 不支持身份验证。
编辑: 我似乎找不到一种通过套接字将身份验证数据附加到特定代理主机的方法。 Authenticator.setDefault() 只允许一组凭据。
Authenticator.setDefault(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
PasswordAuthentication p=new PasswordAuthentication("xxx", "xxx".toCharArray());
return p;
}
});
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("xxx.xx.xxx.xxx", xxx));
Socket sock = new Socket(proxy);
sock.connect(new InetSocketAddress(server,xx));
I'm looking for a java socks Proxy client class that supports authetication, any suggestions? The java.net.Proxy does not support authentication.
Edit:
I can't seem to find a way that would attach authentication data to particular proxy host via socket. Authenticator.setDefault() allows only one set of credential.
Authenticator.setDefault(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
PasswordAuthentication p=new PasswordAuthentication("xxx", "xxx".toCharArray());
return p;
}
});
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("xxx.xx.xxx.xxx", xxx));
Socket sock = new Socket(proxy);
sock.connect(new InetSocketAddress(server,xx));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 通过注册的
java.net.Authenticator
或通过 网络属性:有关客户端代码示例,您可以检查
编辑:根据评论更新答案
JDK 中 SOCKS 支持的副作用是您的整个 JVM 将通过相同的 SOCKS 代理。所以这可能不适合你。
Authenticator
影响 JVM 中的所有身份验证(HTTP 身份验证、代理身份验证)。再说一次,这可能不适合你。在您的情况下,可能的解决方案是使用 HttpClient 来自 < a href="http://hc.apache.org/" rel="nofollow noreferrer">HttpComponents (遗留 Commons HTTP 客户端 3.x)。查看 示例,尤其是 通过 a 请求代理和代理身份验证示例。
Java supports Socks proxy configuration via a registered
java.net.Authenticator
or via preferences as documented in the Networking Properties:For client code examples, you can check this answer on Stack Overflow.
EDIT: Update of the answer as per comment
The side effect of the SOCKS support in JDK is that your whole JVM will go through the same SOCKS proxy. So this may not work for you.
The
Authenticator
affects all authentication in your JVM (HTTP auth, Proxy Auth). So again, this may not work for you.In your case, a possible solution would be to use HttpClient from HttpComponents (the successor of the legacy Commons HTTP Client 3.x). Check out the samples and especially the Request via a proxy and Proxy authentication examples.
使用此 HttpClient(不是 Apache 的),
http://www.innovation.ch/java/HTTPClient/< /a>
这只是一个 URL 处理程序,因此您可以使用常用的 HTTPUrlConnection。它支持SOCKS和其他代理。
Use this HttpClient (Not Apache's),
http://www.innovation.ch/java/HTTPClient/
This is simply an URL handler so you can use the usual HTTPUrlConnection. It supports SOCKS and other proxy.