I'm trying to set up a Java program, where each thread can use its own proxy.
Right now I only found a way to set a proxy globally. (http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html)
As mentioned earlier, these settings affect all http connections during the entire lifetime of the VM invoked with these options. However it is possible, using the System.setProperty() method, to have a slightly more dynamic behavior.
Here is a code excerpt showing how this can be done:
//Set the http proxy to webcache.mydomain.com:8080
System.setProperty("http.proxyHost", "webcache.mydomain.com"); System.setPropery("http.proxyPort", "8080");
Update
I tried using the proxy class, yet can't create a direct connection for when I don't want to use said proxy:
private void setProxy()
{
if(proxyUrl != null)
{
SocketAddress addr = new InetSocketAddress(proxyUrl, proxyPort);
proxy = new Proxy(Proxy.Type.HTTP, addr);
}
else
{
proxy = new Proxy(Proxy.Type.DIRECT, null);
}
}
Exception in .... java.lang.IllegalArgumentException: type DIRECT is not compatible with address null
How can I get this to work for direct connections? Haven't tried proxies yet.