Java - DefaultHttpClient 和“Host”标头 [Apache HttpComponent]
我通过 DefaultHttpClient 提交多个 HTTP 请求。问题是请求中从未设置“Host”标头。例如,通过执行以下 GET 请求:
HttpUriRequest request = new HttpGet("http://www.myapp.com");
org.apache.http.client.HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse = client.execute(request);
生成的请求对象未设置具有以下值的强制“Host”标头:
Host: myapp.com
有任何提示吗?
I'm submitting multiple HTTP Requests via a DefaultHttpClient. The problem is that the "Host" header is never set in the request. For example by executing the following GET request:
HttpUriRequest request = new HttpGet("http://www.myapp.com");
org.apache.http.client.HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse = client.execute(request);
The generated request object doesn't set the mandatory "Host" header with the value:
Host: myapp.com
Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的错。实际上,
DefaultHttpClient
确实按照 HTTP 规范的要求添加了Host
标头。我的问题是由于我之前添加的另一个自定义标头,其值以
“<代码>\r\n”。这使得由
DefaultHttpClient
自动添加的所有后续标头失效。我正在做类似的事情:
在 HTTP 请求中生成以下标头序列:
X-Custom-Header
和Host
之间的空格使Host
无效> 标题。固定为:
生成:
My fault. Actually the
DefaultHttpClient
do adds theHost
header, as required by the HTTP specification.My problem was due to an other custom header I was adding before whose value ended with
"
\r\n
". This has invalidated all the subsequent headers added automatically byDefaultHttpClient
.I was doing something like:
that generated the following Header sequence in the HTTP request:
The space between
X-Custom-Header
andHost
invalidated theHost
header.Fixed with:
That generates:
只需使用 addHeader。
Just set the host header on the request using addHeader.