WebHeaderCollection.Add 方法

定义

将新的标头插入集合中。

重载

Add(String)

将指定的标头插入集合中。

Add(HttpRequestHeader, String)

将包含指定值的指定标头插入到集合中。

Add(HttpResponseHeader, String)

将包含指定值的指定标头插入到集合中。

Add(String, String)

将具有指定名称和值的标头插入到集合中。

Add(String)

Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs
Source:
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 不包含冒号 (:) 字符。

value 的长度大于 65535。

- 或 -

header 的名称部分为 Empty,或者包含无效字符。

- 或 -

header 是应由属性设置的受限制的标头。

- 或 -

header 的值部分包含无效字符。

仅限 .NET Framework 和 .NET Core 2.0 - 3.1 版本:冒号 (:) 后的字符串的长度大于 65535。

示例

以下示例使用 Add 方法将名称/值对添加到 。WebHeaderCollection

try
{
   //Create a web request for S"www.msn.com".
   HttpWebRequest^ myHttpWebRequest = dynamic_cast<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 = dynamic_cast<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 : {0}", e->Message );
   if ( e->Status == WebExceptionStatus::ProtocolError )
   {
      Console::WriteLine( "Status Code : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusCode );
      Console::WriteLine( "Status Description : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusDescription );
      Console::WriteLine( "Server : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->Server );
   }
}
catch ( Exception^ e ) 
{
   Console::WriteLine( "Exception is thrown. Message is : {0}", e->Message );
}
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)

Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs
Source:
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)

Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs
Source:
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)

Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs
Source:
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。

示例

以下示例使用 Add 方法将名称/值对添加到 。WebHeaderCollection

try
{
   //Create a web request for S"www.msn.com".
   HttpWebRequest^ myHttpWebRequest = dynamic_cast<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 = dynamic_cast<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 : {0}", e->Message );
   if ( e->Status == WebExceptionStatus::ProtocolError )
   {
      Console::WriteLine( "Status Code : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusCode );
      Console::WriteLine( "Status Description : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusDescription );
      Console::WriteLine( "Server : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->Server );
   }
}
catch ( Exception^ e ) 
{
   Console::WriteLine( "Exception is thrown. Message is : {0}", e->Message );
}
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 中验证。

注解

如果 中指定的 name 标头不存在,该方法会将 Add 新的标头插入标头名称/值对的列表中。

如果 中 name 指定的标头已存在, value 则会添加到与 name关联的现有以逗号分隔的值列表中。

适用于