Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell java to use a specific outgoing ip interface for a http request?

Tags:

java

http

sockets

Does anybody know a quick way to force an outgoing http request to go through a specific (logical) ip address, in java?

I'm thinking of using Apache HTTP client (part of http components), which surely enough has a way to do it, but the API looks not trivial. Has anybody already performed anything similar with it?

Thank you.

like image 677
AndreaG Avatar asked Nov 29 '25 22:11

AndreaG


2 Answers

Does this help?

How to make the JVM use a given source IP by default?

like image 61
David Soroko Avatar answered Dec 01 '25 11:12

David Soroko


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

bindpoint and endpoint can be InetSocketAddress

like image 31
krzydyn Avatar answered Dec 01 '25 11:12

krzydyn