Hi roddik. All you need to do is set the appropriate system properties before creating your WebClient
object. For example:
System.setProperty("socksProxyHost", "localhost"); // replace "localhost" with your proxy server
System.setProperty("socksProxyPort", "9999"); // replace "9999" with your proxy port number
WebClient client = new WebClient();
At this point, HttpClient (which is used by HtmlUnit under the covers) will pick up the settings and use the SOCKS proxy for all network communication.
UPDATE: I read your revised question (and your comment) and I think you're on the right track. The problem is that if you implement step 1 using the above system properties, then your code is not thread-safe (because those system properties are global). One solution is to synchronize on something, but of course this can introduce performance problems (may not matter to you).
If you really want to control this in a per-socket basis, then I think you will need to do something like the following:
- Create a custom
ProtocolSocketFactory
that passes a java.net.Proxy
object to the Socket
constructor (like in this example).
- Create a custom
Protocol
that uses this ProtocolSocketFactory
.
- Apply this
Protocol
to the new connections in your custom connection manager using HttpConnection.setProtocol()
.
I haven't actually tested this, but based on a quick glance at the HttpClient 3.1 source code, I think that's how it would be done. I would love to hear how you ultimately solve this problem :-). Good luck!