2 Answers
2

HtmlUnit uses HttpClient as the underlying connection library, I investigated this a little, but:

1- Couldn't find a way to configure HttpClient (except by the generic Java Socks mechanism defined in http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html)
2- Do not have access to a public Socks Proxy to test against

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!