HttpWebRequest.Host Property

Get or set the Host header value to use in an HTTP request independent from the request URI.

Namespace:  System.Net
Assembly:  System (in System.dll)

public string Host { get; set; }

Property Value

Type: System.String
The Host header value in the HTTP request.

ExceptionCondition
ArgumentNullException

The Host header cannot be set to null.

ArgumentException

The Host header cannot be set to an invalid value.

InvalidOperationException

The Host header cannot be set after the HttpWebRequest has already started to be sent.

The Host property can be used to set the Host header value to use in an HTTP request independent from the request URI. The Host property can consist of a hostname and an optional port number. A Host header without port information implies the default port for the service requested (port 80 for an HTTP URL, for example).

The format for specifying a host and port must follow the rules in section 14.23 of RFC2616 published by the IETF. An example complying with these requirements that specifies a port of 8080 would be the following value for the Host property:

www.contoso.com:8080

Using the Host property to explicitly specify a custom Host header value also affects areas caching, cookies, and authentication. When an application provides credentials for a specific URI prefix, the applications needs to make sure to use the URI containing the value of the Host header, not the target server in the URI. The key used when caching resources, uses the Host header value rather than the request URI. Cookies are stored in a CookieContainer and logically grouped by the server domain name. If the application specifies a Host header, then this value will be used as domain.

If the Host property is not set, then the Host header value to use in an HTTP request is based on the request URI.

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft