当我的客户端计算机上有多个NIC时,如何将(当然是出站)HttpUrlConnection绑定到特定的本地IP地址? [英] How can I bind an (outbound, of course) HttpUrlConnection to a specific local ip address, when I have multiple NIC on my client machine?

查看:166
本文介绍了当我的客户端计算机上有多个NIC时,如何将(当然是出站)HttpUrlConnection绑定到特定的本地IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种Http-Gateway应用程序,它作为本地网络外服务器的http客户端。

I have a kind of Http-Gateway app, which acts as an http client towards servers outside our local network.

将会有一个网络配置升级,我将遇到问题因为:

- 客户端机器上有多个网卡

- 防火墙/ nat规则使用硬连线IP地址

There is going to be a network configuration upgrade, and I'm going to have problems because:
- there are multiple network cards on client machine
- firewall/nat rules use hardwired ip addresses

如果我能以编程方式强制HttpUrlConnection对象使用特定的ip地址,我会没事的。但是我担心它无法完成。

If I could programmatically force HttpUrlConnection object to use a specific ip address, I would be ok. But I'm afraid it can't be done.

我是对的吗?如果没有,哪个版本的JRE支持它?

Am I right? If not, which version of JRE supports it?

其他可能的解决方案,最好不涉及从头开始重写所有内容?

更简单更好:我知道,有Apache HttpClient,或者我可以使用套接字......

Other possible solutions, preferably ones which don't involve rewriting everything from scratch?
The simpler the better: I know, there is Apache HttpClient, or I could use Sockets...

谢谢

推荐答案

我看不到好的解决方案,但有两个可能性很小:

I can see no good solution but have two poor possibilities:


  1. 代理连接本地:

  1. Proxy the connections locally:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
        "proxy", 5555));
URLConnection urlConnection = url.openConnection(proxy);


  • 注册自定义 URLStreamHandlerFactory 的java.net.URL 。每当在 URL 上调用 openConnection()时,它将由此注册的自定义工厂处理,让您可以控制细节套接字连接。要么使用Apache的实现,要么自己动手?

  • Register a custom URLStreamHandlerFactory with java.net.URL. Whenever openConnection() is called on the URL it would be handled by this registered custom factory, giving you control over details of the socket connection. Either use Apache's implementation or roll your own?

    Url.setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
    


  • 这篇关于当我的客户端计算机上有多个NIC时,如何将(当然是出站)HttpUrlConnection绑定到特定的本地IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆