12

My code is like the following:

URL url = new URL("1.0.0.25/otfg/services");
HttpURLConnection cnx = url.openConnection();
cnx.setRequestProperty("Host", "example.org");

But when I log the outgoing packages with tcpdump the http-header "Host" is 1.0.0.25.

Could it be that the http-header "Host" is overwritten at a later stage of the sending process, if yes how can I avoid this behavior.

Alex
  • 4,033
  • 9
  • 37
  • 52
  • 1
    Why don't you use the hostname in the URL? –  Feb 01 '12 at 14:00
  • I use iptables to alter the source ip address of the outgoing packages, iptables use the fake ip address 1.0.0.25 to find packages which must be altered. The remote service need's the correct domain name for doing a name based virtual hosting service. – Alex Feb 01 '12 at 14:28
  • 1
    Duplicate of http://stackoverflow.com/questions/7648872/can-i-override-the-host-header-where-using-javas-httpurlconnection-class – Gray Feb 01 '12 at 15:01
  • Possible duplicate of [Can I override the Host header where using java's HttpUrlConnection class?](http://stackoverflow.com/questions/7648872/can-i-override-the-host-header-where-using-javas-httpurlconnection-class) – albciff Mar 28 '17 at 07:58

1 Answers1

28

Duplicate question.

The last comment solved my problem:

System.setProperty("sun.net.http.allowRestrictedHeaders", "true")

Or at VM startup:

-Dsun.net.http.allowRestrictedHeaders=true
Community
  • 1
  • 1
Alex
  • 4,033
  • 9
  • 37
  • 52