Java - DefaultHttpClient 和“Host”标头 [Apache HttpComponent]

发布于 2024-11-26 21:39:38 字数 381 浏览 2 评论 0原文

我通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃→可爱长大的 2024-12-03 21:39:38

我的错。实际上,DefaultHttpClient 确实按照 HTTP 规范的要求添加了 Host 标头。

我的问题是由于我之前添加的另一个自定义标头,其值以
“<代码>\r\n”。这使得由 DefaultHttpClient 自动添加的所有后续标头失效。
我正在做类似的事情:

HttpUriRequest request = new HttpGet("http://www.myapp.com");
org.apache.http.client.HttpClient client = new DefaultHttpClient();
request.addHeader(new BasicHeader("X-Custom-Header", "Some Value\r\n");
HttpResponse httpResponse = client.execute(request);

在 HTTP 请求中生成以下标头序列:

GET /index.html HTTP/1.1
X-Custom-Header: Some value

Host: www.example.com

X-Custom-HeaderHost 之间的空格使 Host 无效> 标题。
固定为:

HttpUriRequest request = new HttpGet("http://www.myapp.com");
org.apache.http.client.HttpClient client = new DefaultHttpClient();
request.addHeader(new BasicHeader("X-Custom-Header", "Some Value");
HttpResponse httpResponse = client.execute(request);

生成:

GET /index.html HTTP/1.1
X-Custom-Header: Some value
Host: www.example.com

My fault. Actually the DefaultHttpClient do adds the Host 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 by DefaultHttpClient.
I was doing something like:

HttpUriRequest request = new HttpGet("http://www.myapp.com");
org.apache.http.client.HttpClient client = new DefaultHttpClient();
request.addHeader(new BasicHeader("X-Custom-Header", "Some Value\r\n");
HttpResponse httpResponse = client.execute(request);

that generated the following Header sequence in the HTTP request:

GET /index.html HTTP/1.1
X-Custom-Header: Some value

Host: www.example.com

The space between X-Custom-Header and Host invalidated the Host header.
Fixed with:

HttpUriRequest request = new HttpGet("http://www.myapp.com");
org.apache.http.client.HttpClient client = new DefaultHttpClient();
request.addHeader(new BasicHeader("X-Custom-Header", "Some Value");
HttpResponse httpResponse = client.execute(request);

That generates:

GET /index.html HTTP/1.1
X-Custom-Header: Some value
Host: www.example.com
安人多梦 2024-12-03 21:39:38

只需使用 addHeader

Just set the host header on the request using addHeader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文