支持身份验证的Java代理客户端类

前端之家收集整理的这篇文章主要介绍了支持身份验证的Java代理客户端类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在寻找支持认证的java socks Proxy客户端类,有什么建议吗? 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));
最佳答案
Java通过已注册的java.net.Authenticator或通过Networking Properties中记录的首选项支持Socks代理配置:

SOCKS protocol support settings

The SOCKS username and password are
acquired in the following way. First,
if the application has registered a
java.net.Authenticator default
instance,then this will be queried
with the protocol set to the string
“SOCKS5”,and the prompt set to to the
string “SOCKS authentication”. If the
authenticator does not return a
username/password or if no
authenticator is registered then the
system checks for the user preferences
java.net.socks.username” and
java.net.socks.password“. If these
preferences do not exist,then the
system property “user.name” is checked
for a username. In this case,no
password is supplied.

socksProxyHost
socksProxyPort (default: 1080)
Indicates the name of the SOCKS proxy server and the port number that will be used by the SOCKS protocol layer. If socksProxyHost is specified then all TCP sockets will use the SOCKS proxy server to establish a connection or accept one. The SOCKS proxy server can either be a SOCKS v4 or v5 server and it has to allow for unauthenticated connections.

对于客户端代码示例,您可以在Stack Overflow上查看this answer.

编辑:根据评论更新答案

JDK中SOCKS支持的副作用是您的整个JVM将通过相同的SOCKS代理.所以这可能不适合你.

Authenticator会影响JVM中的所有身份验证(HTTP身份验证,代理身份验证).所以,这可能不适合你.

在您的情况下,可能的解决方案是使用HttpComponents HttpClient(传统Commons HTTP Client 3.x的后继者).查看samples,尤其是Request via a proxyProxy authentication示例.

猜你在找的Java相关文章