如何在HttpURLConnection中覆盖http-header"Host"?

Ale*_*lex 9 java header httpurlconnection

我的代码如下:

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

但是当我使用tcpdump记录传出包时,http-header"Host"是1.0.0.25.

可能是在发送过程的后期阶段覆盖了http-header"Host",如果是,我该如何避免这种行为.

Ale*_*lex 20

重复的问题.

最后的评论解决了我的问题:

System.setProperty("sun.net.http.allowRestrictedHeaders", "true")
Run Code Online (Sandbox Code Playgroud)

或者在VM启动时:

-Dsun.net.http.allowRestrictedHeaders=true
Run Code Online (Sandbox Code Playgroud)