How can I bind an (outbound, of course) HttpUrlConnection to a specific local ip address, when I have multiple NIC on my client machine?

后端 未结 3 1213
梦谈多话
梦谈多话 2021-01-06 06:28

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

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-06 06:59

    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.

提交回复
热议问题