How can I tell java to use a specific outgoing IP interface for an HTTP request?

Does anyone know a quick way to make an outgoing HTTP request go through a specific (logical) IP address in java?

I'm thinking of using the Apache HTTP client (part of the http components), which certainly has a way to do this, but the API looks trivial. Has anyone already done something similar with him?

Thank.

+3
source share
3 answers

use socket.bind(bindpoint)immediately before socket.connect(endpoint).

bindpointand endpointmaybeInetSocketAddress

+1
source

http.route.local-addressparameter is your friend [1]. Alternatively, you can implement a custom HttpRoutePlannerone to have full control over the route calculation process and assign connections to local interfaces using any strategy.

[1] http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e501

0
source

All Articles