HttpWebRequest.Host 屬性

定義

取得或設定 Host 標頭值,以用在獨立於要求 URI 的 HTTP 要求中。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

屬性值

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:

www.contoso.com:8080

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

適用於