Bewerken

Share via


HttpWebRequest.Host Property

Definition

Gets or sets the Host header value to use in an HTTP request independent from the request URI.

public:
 property System::String ^ Host { System::String ^ get(); void set(System::String ^ value); };
public string Host { get; set; }
member this.Host : string with get, set
Public Property Host As String

Property Value

The Host header value in the HTTP request.

Exceptions

The Host header cannot be set to null.

The Host header cannot be set to an invalid value.

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

Remarks

Caution

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete, and you shouldn't use them for new development. Use HttpClient instead.

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.

Applies to