¿Cómo sobrescribir el encabezado http "Host" en una HttpURLConnection?

Mi código es como el siguiente:

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.

preguntado el 01 de febrero de 12 a las 13:02

Why don't you use the hostname in the URL? -

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. -

1 Respuestas

Duplicate question.

The last comment solved my problem:

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

Or at VM startup:

-Dsun.net.http.allowRestrictedHeaders=true

contestado el 23 de mayo de 17 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.