3

How is it possible to bind to specific interface when using java.net.http.HttpClient (Java 11)?

When I was using Apache HttpClient, it was setLocalAddress method.

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
Xdg
  • 1,735
  • 2
  • 27
  • 42
  • Outside of the crude controls here https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html (IPv4 vs IPv6), it isn't clear this is possible. – Yuri Schimke Feb 09 '19 at 10:02
  • If you have a working solution for Apache HttpClient why do you want to change it? – Robert Feb 09 '19 at 12:07
  • You're right that I can use current solution with Apache HttpClient and yes, I'm using it. But I wanted to switch to "native" Java 11 HTTP client and get rid of Apache HttpClient. Unfortunately it isn't possible. – Xdg Feb 09 '19 at 14:04
  • AFAIK the native HTTP client was based on the Apache one. If you think some functionality is missing, consider bringing it up on the `net-dev@openjdk.java.net` mailing list. – Jorn Vernee Feb 09 '19 at 15:20
  • You could set https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/Socket.html#setSocketImplFactory(java.net.SocketImplFactory) but it's a terrible solution – Yuri Schimke Feb 09 '19 at 20:36
  • From what I have read here on SO the Java 11 HttpClient is IMHO not yet ready for production. Too many features missing or not fully working. Give it another year or two and it may be useful. – Robert Feb 10 '19 at 13:18
  • I have logged https://bugs.openjdk.java.net/browse/JDK-8218757 – daniel Feb 11 '19 at 17:14
  • @Xdg Can I please ask why you need such a feature? Is it to support routing on a multi-homed machine? Why can the routing table not be used to determine the outbound interface to use? – chegar999 Feb 11 '19 at 17:34
  • Just use routing is frequest advice. But when you have one server with multiple IP addresses, where each of them has another public IP and you want to be seen with one of these addresses, bind application to specific interface (IP) is the answer. – Xdg Feb 11 '19 at 21:18
  • 1
    @Xdg An API point to support such functionality similar to _builder.setLocalAddress(InetAddress)_ would result in selecting both the outbound interface to use ( as you would expect ), and also the IP protocol version ( IPv4 or IPv6 ). The latter seems less desirable, more of an unintended consequence. – chegar999 Feb 14 '19 at 11:10

0 Answers0