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?
My fault. Actually the
DefaultHttpClientdo adds theHostheader, 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-HeaderandHostinvalidated theHostheader.Fixed with:
That generates: