1

I'm working on a project that have the following need:

  • use source ip address A to access http service S on Remote Server XX.YY.ZZ.WW
  • use source ip address B to access http service T on Remote Server XX.YY.ZZ.WW(same as above)

XX.YY.ZZ.WW is a host I have no control of.

My server is configured with both IP A and IP B on the same Ethernet Interface. My project uses Apache HttpClient. It can be changed to something else if necessary.

Based on my TCP/IP knowledge, this is very easy. As long as I own the IP, I should be able to change the source IP address to whatever I want. But after all, I'm not manipulating IP packet directly. And I have no idea how this can be done with HttpClient.

Haozhun
  • 6,331
  • 3
  • 29
  • 50

2 Answers2

6
final DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, InetAddress.getByName(IP_ADDRESS));
5

You just need to tell HttpClient which network interface to use. You can do this with a connection property:

ConnRoutePNames.LOCAL_ADDRESS='ADDRESS A';

check out section 2.4 of the docs for a full description.

Chris
  • 22,923
  • 4
  • 56
  • 50
  • The doc says "to be used by all default route planner". How can I use different route planner for different instances of httpclient? Thank you! – Haozhun Nov 21 '11 at 14:57
  • You'd need to have 2 different HttpClient instances, each configured with your preferred local address. – Chris Nov 21 '11 at 15:00
  • Actually, I found the answer myself in documentation. I didn't know it should actually be configured on HttpParams. I thought it would be on something more complicated. Thank you! – Haozhun Nov 21 '11 at 15:08
  • This seems to be STATIC. well what If I have 2 threads for each of connections explained as above? this could cause problem, right ? – Sepehr GH Jan 30 '17 at 07:27