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.

2 Answers 2

6
final DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, InetAddress.getByName(IP_ADDRESS));
Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

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!
You'd need to have 2 different HttpClient instances, each configured with your preferred local address.
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!
This seems to be STATIC. well what If I have 2 threads for each of connections explained as above? this could cause problem, right ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.