HttpListenerResponse.AddHeader(String, String) HttpListenerResponse.AddHeader(String, String) HttpListenerResponse.AddHeader(String, String) HttpListenerResponse.AddHeader(String, String) Method

定義

將指定的標頭和值加入至這個回應的 HTTP 標頭。Adds the specified header and value to the HTTP headers for this response.

public:
 void AddHeader(System::String ^ name, System::String ^ value);
public void AddHeader (string name, string value);
member this.AddHeader : string * string -> unit
Public Sub AddHeader (name As String, value As String)

參數

name
String String String String

要設定的 HTTP 標頭名稱。The name of the HTTP header to set.

value
String String String String

name 標頭的值。The value for the name header.

例外狀況

namenull 或空字串 ("")。name is null or an empty string ("").

您不可指定該特定標頭的值。You are not allowed to specify a value for the specified header.

-或--or- namevalue 包含無效字元。name or value contains invalid characters.

value 的長度大於 65,535 個字元。The length of value is greater than 65,535 characters.

範例

下列程式碼範例示範如何將使用這個屬性的標頭。The following code example demonstrates adding a header using this property.

public static void SetExpirationDate(long seconds, HttpListenerResponse response)
{
    response.AddHeader("Expires", seconds.ToString());
}
Public Shared Sub SetExpirationDate(ByVal seconds As Long, ByVal response As HttpListenerResponse)
    response.AddHeader("Expires", seconds.ToString())
End Sub

備註

呼叫這個方法就相當於呼叫Set方法所傳回的集合上Headers屬性。Calling this method is equivalent to calling the Set method on the collection returned by the Headers property.

如果在指定的標頭name不存在,這個方法會插入新的標頭至Headers屬性的集合。If the header specified in name does not exist, this method inserts a new header into the Headers property's collection. 如果name存在於集合中,現有值與這個方法會取代valueIf name is present in the collection, this method replaces the existing value with value. 若要新增至現有的標頭的值,而不取代現有的值,使用AppendHeader方法。To add a value to an existing header without replacing the existing value, use the AppendHeader method.

適用於

另請參閱