views:

39

answers:

2

Hi. How can I set the SOCKS proxy for a URLConnection given by (new URL(url)).openConnection()?

Different proxies are needed on a per-connection basis, so please don't suggest setting system properties.

A: 

You can try commons-httpclient:

 httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
Bozho
Are you sure it will work for `SOCKS`? In the code you provided, does `httpclient` automatically recognize, whether the proxy is HTTP, SOCKS4 or SOCKS5?
roddik
I'm not - try it :)
Bozho
+2  A: 

If you want to use different proxy settings on a per-connection bases, you can use the Java 6.0 ProxySelector mechanism as described in Java Networking and Proxies. Specifically, read sections 3 and 4.

Stephen C
Just tried, everything works great. Thanks
roddik