How can I specify the local address on a java.net.URLConnection?
问题 My Tomcat instance is listening to multiple IP addresses, but I want to control which source IP address is used when opening a URLConnection . How can I specify this? 回答1: 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