HttpWebRequest.Host 屬性


取得或設定 Host 標頭值,以用在獨立於要求 URI 的 HTTP 要求中。Gets or sets the Host header value to use in an HTTP request independent from the request URI.

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



HTTP 要求中的 Host 標頭值。The Host header value in the HTTP request.


主機標頭無法設定為 nullThe Host header cannot be set to null.

主機標頭無法設定為無效值。The Host header cannot be set to an invalid value.

HttpWebRequest 已經開始傳送之後,則無法設定主機標頭。The Host header cannot be set after the HttpWebRequest has already started to be sent.


Host屬性可以用來設定主機標頭值,以在與要求 URI 無關的 HTTP 要求中使用。The Host property can be used to set the Host header value to use in an HTTP request independent from the request URI. Host屬性可以包含主機名稱和選擇性的埠號碼。The Host property can consist of a hostname and an optional port number. 不含埠資訊的主機標頭意指標對 HTTP URL (埠80所要求之服務的預設埠,例如) 。A Host header without port information implies the default port for the service requested (port 80 for an HTTP URL, for example).

指定主機和埠的格式必須遵循 IETF 所發佈之 RFC2616 第14.23 節中的規則。The format for specifying a host and port must follow the rules in section 14.23 of RFC2616 published by the IETF. 符合這些需求的範例會指定埠8080,這會是屬性的下列值 HostAn example complying with these requirements that specifies a port of 8080 would be the following value for the Host property:

使用 Host 屬性明確指定自訂主機標頭值也會影響區域快取、cookie 和驗證。Using the Host property to explicitly specify a custom Host header value also affects areas caching, cookies, and authentication. 當應用程式提供特定 URI 前置詞的認證時,應用程式必須確保使用包含主機標頭值的 URI,而不是 URI 中的目標伺服器。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. 快取資源時使用的索引鍵,會使用主機標頭值,而不是要求 URI。The key used when caching resources, uses the Host header value rather than the request URI. Cookie 會儲存在中 CookieContainer ,並以邏輯方式依伺服器功能變數名稱分組。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.

如果 Host 未設定屬性,則在 HTTP 要求中使用的主機標頭值會以要求 URI 為基礎。If the Host property is not set, then the Host header value to use in an HTTP request is based on the request URI.