当我的客户端计算机上有多个 NIC 时,如何将(当然是出站)HttpUrlConnection 绑定到特定的本地 IP 地址?

发布于 2024-08-10 19:07:40 字数 330 浏览 2 评论 0原文

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

网络配置将会升级,我会遇到问题,因为:
- 客户端机器上有多个网卡
- 防火墙/nat 规则使用硬连线 IP 地址

如果我可以以编程方式强制 HttpUrlConnection 对象使用特定的 IP 地址,我就可以了。但恐怕做不到。

我说得对吗?如果不支持,哪个版本的 JRE 支持?

其他可能的解决方案,最好是不涉及从头开始重写所有内容的解决方案?
越简单越好:我知道,有 Apache HttpClient,或者我可以使用套接字...

谢谢

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

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

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.

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

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...

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-08-17 19:07:40

你不能。 HttpURLConnection 不提供任何操作套接字的接口。

你也不应该这样做。接口的选择基于路由决策。如果您必须手动选择网卡才能到达目的地,那么您的所有互联网连接都会遇到同样的问题。您应该在操作系统中添加静态路由,以确保目的地使用正确的接口。

You can't. HttpURLConnection doesn't provide any interface to manipulate the socket.

You shouldn't do this either. Selection of interface is based on routing decision. If you have to manually select the NIC to get to the destination, all your internet connection would have the same issue. You should add static route in the OS to make sure the correct interface is used for the destination.

窗影残 2024-08-17 19:07:40

我看不出有什么好的解决方案,但有两种可能:

  1. 在本地代理连接:

    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
            “代理”,5555));
    URLConnection urlConnection = url.openConnection(proxy);
    
  2. 使用 java.net.URL 注册自定义 URLStreamHandlerFactory。每当在 URL 上调用 openConnection() 时,它都会由这个注册的自定义工厂处理,让您可以控制套接字连接的详细信息。使用 Apache 的实现还是自己开发?

    Url.setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
    

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

  1. Proxy the connections locally:

    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
            "proxy", 5555));
    URLConnection urlConnection = url.openConnection(proxy);
    
  2. 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)
    
平生欢 2024-08-17 19:07:40

Socket 类有一个构造函数 ( http: //java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html ),它可以让您指定 localAddr。这使得您可以在 Java 中执行您想要的操作。

不幸的是,HttpUrlConnection 及其类系列并没有给您太多机会来获取底层套接字。我遵循了 setContentHandlerFactory 提供的可能性,但这只能让您在套接字打开后很长时间内操作内容。

因此,我的建议是让管理员改变您计算机的路由表,以便从该计算机到目标主机的唯一可能(或最佳)路由将通过您要使用的网关。这样,当打开套接字连接时,它将默认使用该网关的卡。

There's a constructor on the Socket class ( http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html ) that will let you specify a localAddr. This makes it possible to do what you want in Java.

Unfortunately, HttpUrlConnection and its family of classes don't give you much opportunity to get at the underlying socket. I followed the possibility offered by setContentHandlerFactory but that only lets you manipulate the content long after the socket's been opened.

My suggestion, therefore, would be to twist your admin's arm into changing the routing table of your machine such that the only one possible (or optimal) route from that machine to your target hosts will go through the gateway you want to use. That way, when a socket connection is opened it will default to the card for that gateway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文