如何在java.net.URLConnection上指定本地地址? [英] How can I specify the local address on a java.net.URLConnection?
本文介绍了如何在java.net.URLConnection上指定本地地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的Tomcat实例正在侦听多个IP地址,但我想控制打开 URLConnection
时使用的源IP地址。
My Tomcat instance is listening to multiple IP addresses, but I want to control which source IP address is used when opening a URLConnection
.
我该如何指定?
推荐答案
这应该做的技巧:
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.
啊我省略了导入语句
这篇关于如何在java.net.URLConnection上指定本地地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文