3

Does anybody know a quick way to force an outgoing http request to go through a specific (logical) ip address, in java?

I'm thinking of using Apache HTTP client (part of http components), which surely enough has a way to do it, but the API looks not trivial. Has anybody already performed anything similar with it?

Thank you.

AndreaG
  • 1,106
  • 2
  • 12
  • 28
  • I would have thought that this was determined from your host's routing table rather than any higher level layer. – Nick Mar 21 '11 at 10:59
  • @NIck, Linux, uses a Weak Host model which means it takes the routing table rather than the "hint" provided by Java. However Windows uses the Strong Hosting Model which means it will use the IP address set for the ServerSocket. – Peter Lawrey Mar 21 '11 at 11:03

3 Answers3

2

Does this help?

How to make the JVM use a given source IP by default?

Community
  • 1
  • 1
David Soroko
  • 8,521
  • 2
  • 39
  • 51
1

use socket.bind(bindpoint) just before socket.connect(endpoint).

bindpoint and endpoint can be InetSocketAddress

j0k
  • 22,600
  • 28
  • 79
  • 90
krzydyn
  • 11
  • 1
0

http.route.local-address parameter is your friend [1]. Alternatively you might want to implement a custom HttpRoutePlanner in order to have full control over the process of route computation and to assign connections to local interfaces using a strategy of some sort.

[1] http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e501

ok2c
  • 26,450
  • 5
  • 63
  • 71