Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HttpWebRequest and Host header

1,171 views
Skip to first unread message

Mark

unread,
Mar 24, 2005, 12:19:01 PM3/24/05
to
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

mwi...@gmail.com

unread,
Mar 24, 2005, 12:38:21 PM3/24/05
to
Use the Sockets class (instead of HTTPWebRequest) and you'll have full
control over what headers get sent and what values to send.

Feroze [msft]

unread,
Mar 24, 2005, 2:46:12 PM3/24/05
to
You cannot set the host header, unfortunately.

--
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.
----------------

"Mark" <mmod...@nospam.nospam> wrote in message
news:AD5BD471-3EFE-49B8...@microsoft.com...

Dave P.

unread,
Mar 25, 2005, 3:07:40 PM3/25/05
to
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)


"Mark" <mmod...@nospam.nospam> wrote in message
news:AD5BD471-3EFE-49B8...@microsoft.com...

Dave P.

unread,
Mar 25, 2005, 3:44:42 PM3/25/05
to
Oops. That should read WebProxy, not CustomWebProxy.

"Dave P." <dpurrin...@mailnull.com> wrote in message
news:u25%23wZXMF...@TK2MSFTNGP09.phx.gbl...

Feroze [msft]

unread,
Mar 25, 2005, 6:38:35 PM3/25/05
to
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.
----------------

"Dave P." <dpurrin...@mailnull.com> wrote in message
news:#1$yduXMF...@TK2MSFTNGP09.phx.gbl...

0 new messages