How can I specify the local address on a java.net.URLConnection?

This should do the trick:

URL url = new URL(yourUrlHere);
Proxy proxy = new Proxy(Proxy.Type.DIRECT, 
    new InetSocketAddress( 
        InetAddress.getByAddress(
            new byte[]{your, ip, interface, here}), yourTcpPortHere));
URLConnection conn = url.openConnection(proxy);

And you are done.
Dont forget to handle exceptions nicely and off course change the values to suit your scenario.

Ah and I omitted the import statements

Leave a Comment