开发者

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

开发者 https://www.devze.com 2023-02-19 03:58 出处:网络
Does anybody know a quick way to force an outgoing http request to go through a specific (logical) ip address, in java?

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 simila开发者_如何学JAVAr with it?

Thank you.


Does this help?

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


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

bindpoint and endpoint can be InetSocketAddress


http.route.local-address parameter is your friend [1]. Alternatively you might want to implement a custom HttpRoutePlanner in order to have full control over the process of route computation and to assign connections to local interfaces using a strategy of some sort.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消