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 ?
    – PeterMmm
    Commented Sep 7, 2013 at 12:55
  • Nope, different subnetworks
    – Jhon
    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();
    }
1
  • 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. Commented Jan 15, 2018 at 18:42
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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.