Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

HttpWebRequest::Host Property

.NET Framework 4.6 and 4.5

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:
property String^ Host {
	String^ get ();
	void set (String^ value);
}

Property Value

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

ExceptionCondition
ArgumentNullException

The Host header cannot be set to nullptr.

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.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4
Show:
© 2015 Microsoft