我可以在使用 java 的 HttpUrlConnection 类的地方覆盖 Host 标头吗? [英] Can I override the Host header where using java's HttpUrlConnection class?
问题描述
我正在使用以下代码在 java 中打开一个 http 连接:
I'm using the following code to open a http connection in java:
URL url = new URL("http://stackoverflow.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Host", "Test:8080");
conn.getOutputStream();
但是,无论我调用方法的顺序如何,并且将主机重置为目标服务器,调用 conn.setRequestProperty("Host", "Test:8080") 似乎都不起作用.有没有办法在不使用其他库的情况下覆盖 Host 标头?
However calling conn.setRequestProperty("Host", "Test:8080") appears to have no effect regardless of what order I call the methods and the Host is reset to the destination server. Is there any way to override the Host header without using a different library?
TIA 马特
推荐答案
这在过去可以工作,但作为安全修复的一部分已被禁用.显然在更改日志中没有注释.甚至在 bugs.sun.com 上也存在诸如 #7022056 之类的错误.
This used to work in the past, but it has been disabled as part of a security-fix. Apparently without a note in the changelog. There are even bugs like #7022056 for this at bugs.sun.com.
有一个 类似问题 另一个标题,答案更详细,所以我只是链接它而不是自己写.:-)
There is a similar question for another header, where the answer goes more into the details, so I just link it instead of writing it myself. :-)
唯一的解决方法似乎是将 sun.net.http.allowRestrictedHeaders
设置为 true
或使用另一个 http-library,如已经提到的 http 组件.
The only workarounds seem to be setting sun.net.http.allowRestrictedHeaders
to true
or use another http-library like the already mentioned http components.
这篇关于我可以在使用 java 的 HttpUrlConnection 类的地方覆盖 Host 标头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!