Wurstbro
Wurstbro

Reputation: 974

Each thread using its own proxy

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.

Upvotes: 2

Views: 2755

Answers (2)

Barry NL
Barry NL

Reputation: 963

Maybe use the Proxy class as explained here in section 3:

As we have seen, the system properties are powerful, but not flexible. The "all or nothing" behavior was justly deemed too severe a limitation by most developers. That's why it was decided to introduce a new, more flexible, API in J2SE 5.0 so that it would be possible to have connection based proxy settings.

You could use Proxy.NO_PROXY to:

... not to use any proxying.

Do something like this:

private void setProxy()
{
    if(proxyUrl != null)
    {
        SocketAddress addr = new InetSocketAddress(proxyUrl, proxyPort);
        proxy = new Proxy(Proxy.Type.HTTP, addr);
    }
    else
    {
        proxy = Proxy.NO_PROXY;
    }       
}

Upvotes: 6

Wurstbro
Wurstbro

Reputation: 974

Barry NL's solution semi-worked, since I couldn't figure out how to not use a proxy with that solution.

What I came up with:

proxyUrl and proxyPort are in the constructor of my class.

I wasn't able to call url.openConnection(proxy); if I just set proxy = null

or proxy = new Proxy(Proxy.Type.DIRECT, null); . So I wrote my own openConnection

private void setProxy()
{
    if(proxyUrl != null)
    {
        SocketAddress addr = new InetSocketAddress(proxyUrl, proxyPort);
        proxy = new Proxy(Proxy.Type.HTTP, addr);
    }
    else
    {
        proxy = null;
    }       
}
private URLConnection openConnection2(URL url)
{
    try {
        if(proxy != null)               
            return url.openConnection(proxy);               
        else 
            return url.openConnection();            
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }   
}

setProxy() has to be called before making any connections.

Also I replaced

url = new URL(http_url);
con = (HttpURLConnection)url.openConnection(proxy);

with

url = new URL(http_url);
con = (HttpURLConnection)openConnection2(url);  

Upvotes: 0

Related Questions