我可以覆盖使用java的HttpUrlConnection类的Host头吗? [英] Can I override the Host header where using java's HttpUrlConnection class?

查看:702
本文介绍了我可以覆盖使用java的HttpUrlConnection类的Host头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在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 Matt

推荐答案

过去曾经使用它,但它已作为安全修复程序的一部分被禁用。显然没有更改日志中的注释。在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库,如已经提到的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆