我可以在使用 java 的 HttpUrlConnection 类的地方覆盖 Host 标头吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7648872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:50:30  来源:igfitidea点击:

Can I override the Host header where using java's HttpUrlConnection class?

javahttpnetworkinghttpurlconnectionurlconnection

提问by Matt

I'm using the following code to open a http connection in java:

我正在使用以下代码在 java 中打开一个 http 连接:

 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();

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?

但是,无论我调用方法的顺序如何,并且将主机重置为目标服务器,调用 conn.setRequestProperty("Host", "Test:8080") 似乎都不起作用。有没有办法在不使用其他库的情况下覆盖 Host 标头?

TIA Matt

蒂亚马特

回答by Boris

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 #7022056for this at bugs.sun.com.

这曾经在过去有效,但它已作为安全修复的一部分被禁用。显然在更改日志中没有注释。在bugs.sun.com上甚至还有#7022056 之类的错误。

There is a similar questionfor another header, where the answer goes more into the details, so I just link it instead of writing it myself. :-)

另一个标题有一个类似的问题,答案更多地涉及细节,所以我只是链接它而不是自己编写。:-)

The only workarounds seem to be setting sun.net.http.allowRestrictedHeadersto trueor use another http-library like the already mentioned http components.

唯一的解决办法似乎是设置sun.net.http.allowRestrictedHeaderstrue或使用其他HTTP库,如已经提到的HTTP组件

回答by Bozho

The Hostheader is filled by the HttpURLConnectionbased on the URL. You can't open foo.comwith Host=bar.com. From the RFC

Host头被填充HttpURLConnection基于URL。你不能foo.comHost=bar.com. 来自RFC

The Host request-header field specifies the Internet host and port number of the resource being requested, as obtained from the original URI given by the user or referring resource (generally an HTTP URL)

Host request-header 字段指定了被请求资源的 Internet 主机和端口号,从用户或引用资源(通常是 HTTP URL)给出的原始 URI 中获得

Btw, you can also try apache http components.

顺便说一句,您也可以尝试apache http components

回答by TheCobra

This is an issue with how volley handles HTTPUrlConnection and retry policy.

这是 volley 如何处理 HTTPUrlConnection 和重试策略的问题。

A Quick fix for it is to extend "HurlStack" class and override the "createConnection" function to return a HTTPUrlConnection with ChunkStreamMode of 0

一个快速修复是扩展“HurlStack”类并覆盖“createConnection”函数以返回一个ChunkStreamMode为0的HTTPUrlConnection

public class CustomHurlStack extends HurlStack {


   public CustomHurlStack(){
       super();

   }

   @Override
   protected HttpURLConnection createConnection(URL url) throws IOException {
       HttpURLConnection connection = super.createConnection(url);
       connection.setChunkedStreamingMode(0);
       return connection;
   }

}

}