HttpWebRequest and Host header

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi...

I've been working a bit with the HttpWebRequest class and one requirement
I've gotten recently is the desire to set the HTTP Host: header separately
from the specific machine referenced in the url. For example, the url
http://frontend1.east.company.com
but passing
Host: www.company.com
to emulate a call to the general front door.

I look in the HttpWebRequest documentation on MSDN, and under the Headers
property, it says specifically that the Host header's derived from the url
(implying that it can't be overridden).

Is there any way to do what I want to do?

Thanks
_mark
 
Use the Sockets class (instead of HTTPWebRequest) and you'll have full
control over what headers get sent and what values to send.
 
Mark:

You can accomplish this by using the Proxy property on HttpWebRequest. Use
the host header value as the host in your URL. Then, set your proxy object
to translate the address to your actual target host.

So, using your example:
Uri uri = new Uri("http://www.company.com");

WebRequest request = WebRequest.Create(uri);

request.Proxy = new CustomWebProxy("http://frontend1.east.company.com");

request.GetResponse();

The end result will be to have the host header of www.company.com , but
still be GETting the request from "frontend1.east.company.com" Keep in
mind that that this code will have all of your requests going to the same
host.

Hope that helps,
Dave P.
(dpurrington.ng2ATmailnullDAHTcom)
 
Yes, this will work. However there is a subtle difference in the request
headers sent with this method, and what the actual request should be
according to RFC.

Using the suggested method, the request that will be sent is this:

GET http://www.company.com/test.aspx HTTP/1.1
Host: www.company.com
<other http headers>

What the RFC expects in this situation in this:

GET /test.aspx HTTP/1.1
Host: www.company.com
<other http headers>

As per RFC Section 5.2:
===
An origin server that does differentiate resources based on the host
requested (sometimes referred to as virtual hosts or vanity host
names) MUST use the following rules for determining the requested
resource on an HTTP/1.1 request:

1. If Request-URI is an absoluteURI, the host is part of the
Request-URI. Any Host header field value in the request MUST be
ignored.

===

If a server gets a request with an absolute URI, it is supposed to ignore
the host header. Note that although the host header is being ignored, it
will still work because the absolute URI contains the host you want to send
this to.


--
feroze

-----------------
This posting is provided as-is. It offers no warranties and assigns no
rights.

See http://weblogs.asp.net/feroze_daud for System.Net related posts.
----------------
 
Back
Top