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

前端 未结 5 1320
死守一世寂寞
死守一世寂寞 2021-01-03 04:39

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.

                 


        
5条回答
  •  耶瑟儿~
    2021-01-03 04:53

    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

提交回复
热议问题