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 Answers
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();
}
-
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
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.