共用方式為


WebHeaderCollection.Add 方法

定義

在集合中插入一個新的標頭。

多載

名稱 Description
Add(String)

將指定的標頭插入集合中。

Add(HttpRequestHeader, String)

將指定標頭與指定值插入集合中。

Add(HttpResponseHeader, String)

將指定標頭與指定值插入集合中。

Add(String, String)

將指定名稱與值的標頭插入集合中。

Add(String)

來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs

將指定的標頭插入集合中。

public:
 void Add(System::String ^ header);
public void Add(string header);
override this.Add : string -> unit
Public Sub Add (header As String)

參數

header
String

要加上標頭,名稱和值用冒號分隔。

例外狀況

headernullEmpty

header 不包含冒號(:)字元。

-或-

價值 header 部分的長度大於65535。

-或-

名稱headerEmpty部分包含無效字元。

-或-

header 是一個受限標頭,應該以屬性設定。

-或-

header 部分包含無效字元。

僅 .NET Framework 與 .NET Core 2.0 - 3.1 版本:冒號(:) 後的字串長度大於 65535。

範例

以下範例使用 Method Add 為 加入WebHeaderCollection名稱/值對。

try {
    //Create a web request for "www.msn.com".
    HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("http://www.msn.com");

    //Get the headers associated with the request.
    WebHeaderCollection myWebHeaderCollection = myHttpWebRequest.Headers;

    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method");

    //Add the Accept-Language header (for Danish) in the request.
    myWebHeaderCollection.Add("Accept-Language:da");

    //Include English in the Accept-Langauge header. 
    myWebHeaderCollection.Add("Accept-Language","en;q=0.8");

    //Get the associated response for the above request.
    HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();

    //Print the headers for the request.
    printHeaders(myWebHeaderCollection);
    myHttpWebResponse.Close();
}
//Catch exception if trying to add a restricted header.
catch(ArgumentException e) {
    Console.WriteLine(e.Message);
}
catch(WebException e) {
    Console.WriteLine("\nWebException is thrown. \nMessage is :" + e.Message);
    if(e.Status == WebExceptionStatus.ProtocolError) {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
        Console.WriteLine("Server : {0}", ((HttpWebResponse)e.Response).Server);
    }
}
catch(Exception e) {
    Console.WriteLine("Exception is thrown. Message is :" + e.Message);
}
Public Shared Sub Main()

 Try
        'Create a web request for "www.msn.com".
        Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.msn.com"), HttpWebRequest)
        
        'Get the headers associated with the request.
        Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebRequest.Headers
        
    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method")
        
    'Add the Accept-Language header (for Danish) in the request.
        myWebHeaderCollection.Add("Accept-Language:da")
        
        'Include English in the Accept-Langauge header. 
        myWebHeaderCollection.Add("Accept-Language","en;q" + ChrW(61) + "0.8")
        
        'Get the associated response for the above request.
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        
        'Print the headers for the request.
        printHeaders(myWebHeaderCollection)
        myHttpWebResponse.Close()
    'Catch exception if trying to add a restricted header.
    Catch e As ArgumentException
        Console.WriteLine(e.Message)
    Catch e As WebException
        Console.WriteLine(e.Message)
        If e.Status = WebExceptionStatus.ProtocolError Then
            Console.WriteLine("Status Code : {0}", CType(e.Response, HttpWebResponse).StatusCode)
            Console.WriteLine("Status Description : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
            Console.WriteLine("Server : {0}", CType(e.Response, HttpWebResponse).Server)
        End If
    Catch e As Exception
        Console.WriteLine(e.Message)
    End Try
End Sub

備註

部分header的長度,也就是冒號(:) 後的字串,僅在 .NET Framework 及 .NET Core 2.0 至 3.1 版本中被驗證。

備註

header參數必須以「name:value」格式指定。 若集合中不存在指定的標頭,則會新增一個標頭到集合中。

如果 中 header 指定的標頭已經存在於集合中,則 的 header 值部分會與現有值串接。

適用於

Add(HttpRequestHeader, String)

來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs

將指定標頭與指定值插入集合中。

public:
 void Add(System::Net::HttpRequestHeader header, System::String ^ value);
public void Add(System.Net.HttpRequestHeader header, string? value);
public void Add(System.Net.HttpRequestHeader header, string value);
override this.Add : System.Net.HttpRequestHeader * string -> unit
Public Sub Add (header As HttpRequestHeader, value As String)

參數

header
HttpRequestHeader

要加入收藏的標頭。

value
String

標頭的內容。

例外狀況

僅 .NET Framework 與 .NET Core 2.0 - 3.1 版本:長度 value 超過 65535。

備註

若指定的標頭不存在, Add 該方法會在標頭名稱/值對的清單中插入新的標頭。

若指定的標頭已存在, value 則會加入與標頭相關的逗號分隔值清單中。

備註

value 長度僅在 .NET Framework 及 .NET Core 2.0 至 3.1 版本中進行驗證。

適用於

Add(HttpResponseHeader, String)

來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs

將指定標頭與指定值插入集合中。

public:
 void Add(System::Net::HttpResponseHeader header, System::String ^ value);
public void Add(System.Net.HttpResponseHeader header, string? value);
public void Add(System.Net.HttpResponseHeader header, string value);
override this.Add : System.Net.HttpResponseHeader * string -> unit
Public Sub Add (header As HttpResponseHeader, value As String)

參數

header
HttpResponseHeader

要加入收藏的標頭。

value
String

標頭的內容。

例外狀況

僅 .NET Framework 與 .NET Core 2.0 - 3.1 版本:長度 value 超過 65535。

備註

若指定的標頭不存在, Add 該方法會在標頭名稱/值對的清單中插入新的標頭。

若指定的標頭已存在, value 則會加入與標頭相關的逗號分隔值清單中。

備註

value 長度僅在 .NET Framework 及 .NET Core 2.0 至 3.1 版本中進行驗證。

適用於

Add(String, String)

來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs

將指定名稱與值的標頭插入集合中。

public:
 override void Add(System::String ^ name, System::String ^ value);
public override void Add(string name, string? value);
public override void Add(string name, string value);
override this.Add : string * string -> unit
Public Overrides Sub Add (name As String, value As String)

參數

name
String

要加入收藏的標頭。

value
String

標頭的內容。

例外狀況

namenullEmpty,或包含無效字元。

-或-

name 是一個受限標頭,必須以屬性設定來設定。

-或-

value 包含無效字元。

僅 .NET Framework 與 .NET Core 2.0 - 3.1 版本:長度 value 超過 65535。

範例

以下範例使用 Method Add 為 加入WebHeaderCollection名稱/值對。

try {
    //Create a web request for "www.msn.com".
    HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("http://www.msn.com");

    //Get the headers associated with the request.
    WebHeaderCollection myWebHeaderCollection = myHttpWebRequest.Headers;

    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method");

    //Add the Accept-Language header (for Danish) in the request.
    myWebHeaderCollection.Add("Accept-Language:da");

    //Include English in the Accept-Langauge header. 
    myWebHeaderCollection.Add("Accept-Language","en;q=0.8");

    //Get the associated response for the above request.
    HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();

    //Print the headers for the request.
    printHeaders(myWebHeaderCollection);
    myHttpWebResponse.Close();
}
//Catch exception if trying to add a restricted header.
catch(ArgumentException e) {
    Console.WriteLine(e.Message);
}
catch(WebException e) {
    Console.WriteLine("\nWebException is thrown. \nMessage is :" + e.Message);
    if(e.Status == WebExceptionStatus.ProtocolError) {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
        Console.WriteLine("Server : {0}", ((HttpWebResponse)e.Response).Server);
    }
}
catch(Exception e) {
    Console.WriteLine("Exception is thrown. Message is :" + e.Message);
}
Public Shared Sub Main()

 Try
        'Create a web request for "www.msn.com".
        Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.msn.com"), HttpWebRequest)
        
        'Get the headers associated with the request.
        Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebRequest.Headers
        
    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method")
        
    'Add the Accept-Language header (for Danish) in the request.
        myWebHeaderCollection.Add("Accept-Language:da")
        
        'Include English in the Accept-Langauge header. 
        myWebHeaderCollection.Add("Accept-Language","en;q" + ChrW(61) + "0.8")
        
        'Get the associated response for the above request.
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        
        'Print the headers for the request.
        printHeaders(myWebHeaderCollection)
        myHttpWebResponse.Close()
    'Catch exception if trying to add a restricted header.
    Catch e As ArgumentException
        Console.WriteLine(e.Message)
    Catch e As WebException
        Console.WriteLine(e.Message)
        If e.Status = WebExceptionStatus.ProtocolError Then
            Console.WriteLine("Status Code : {0}", CType(e.Response, HttpWebResponse).StatusCode)
            Console.WriteLine("Status Description : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
            Console.WriteLine("Server : {0}", CType(e.Response, HttpWebResponse).Server)
        End If
    Catch e As Exception
        Console.WriteLine(e.Message)
    End Try
End Sub

備註

value 長度僅在 .NET Framework 及 .NET Core 2.0 至 3.1 版本中進行驗證。

備註

若標頭不存在Addname該方法會在標頭名稱/值對清單中插入新的標頭。

若標頭 name 已存在, value 則加入現有逗號分隔的與 相關 name值清單。

適用於