5

I have machine with 4 internet IP's and I want to know if I can make apache http client to make requests from specific ip/network interface

2
  • The 4 IP's belongs to the same subnet ? Commented Sep 7, 2013 at 12:55
  • Nope, different subnetworks Commented Sep 7, 2013 at 14:10

2 Answers 2

6

Using HttpClient 4.3 APIs

    RequestConfig config = RequestConfig.custom()
            .setLocalAddress(InetAddress.getByAddress(new byte[] {127,0,0,1}))
            .build();
    HttpGet httpGet = new HttpGet("/stuff");
    httpGet.setConfig(config);
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        CloseableHttpResponse response = httpClient.execute(httpGet);
        try {
            // do something useful
        } finally {
            response.close();
        }
    } finally {
        httpClient.close();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Please explain more how this forces the request to go out a specific interface. Does the 127.0.0.1 localhost address have to be replaced with something specific to make it work? I have been given a similar requirement but don't know how to apply your solution. Thx.
0

Never did this, but there is a ClientConnectionOperator interface (and some factories too) in the API to create the socket. Maybe you can implement your own and create the socket with a concrete interface.

Comments

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.