WebHeaderCollection.Add 方法

定義

將新標頭插入至集合。Inserts a new header into the collection.

多載

Add(String)

將指定標頭插入至集合。Inserts the specified header into the collection.

Add(HttpRequestHeader, String)

將具有指定值的指定標頭插入至集合。Inserts the specified header with the specified value into the collection.

Add(HttpResponseHeader, String)

將具有指定值的指定標頭插入至集合。Inserts the specified header with the specified value into the collection.

Add(String, String)

將具有指定名稱和值的標頭插入至集合。Inserts a header with the specified name and value into the collection.

Add(String)

將指定標頭插入至集合。Inserts the specified header into the collection.

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

參數

header
String

要加入的標頭,具有以冒號分隔的名稱和值。The header to add, with the name and value separated by a colon.

例外狀況

headernullEmptyheader is null or Empty.

header 不包含冒號 (:) 字元。header does not contain a colon (:) character.

value 的長度大於 65535。The length of value is greater than 65535.

-或--or- header 的名稱部分是 Empty 或含有無效字元。The name part of header is Empty or contains invalid characters.

-或--or- header 是應該使用屬性設定之受限制的標頭。header is a restricted header that should be set with a property.

-或--or- header 的值部分包含無效字元。The value part of header contains invalid characters.

冒號 (:) 後的字串長度大於 65535。The length the string after the colon (:) is greater than 65535.

範例

下列範例會WebHeaderCollection Add使用方法,將名稱/值組加入至。The following example adds a name/value pair to a WebHeaderCollection using the Add Method.

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參數必須以 "name: value" 的格式指定。The header parameter must be specified in the format "name:value". 如果指定的標頭不存在於集合中,則會將新的標頭加入至集合。If the specified header does not exist in the collection, a new header is added to the collection.

如果集合中所指定header的標頭已存在,則的值部分header會與現有的值串連。If the header specified in header is already present in the collection, the value part of the header is concatenated with the existing value.

Add(HttpRequestHeader, String)

將具有指定值的指定標頭插入至集合。Inserts the specified header with the specified value into the collection.

public:
 void Add(System::Net::HttpRequestHeader header, System::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

要加入至集合的標頭。The header to add to the collection.

value
String

標頭的內容。The content of the header.

例外狀況

value 的長度大於 65535。The length of value is greater than 65535.

這個 WebHeaderCollection 執行個體不允許 HttpRequestHeader 的執行個體。This WebHeaderCollection instance does not allow instances of HttpRequestHeader.

備註

如果指定的標頭不存在,則Add方法會將新的標頭插入標頭名稱/值組的清單中。If the specified header does not exist, the Add method inserts a new header into the list of header name/value pairs.

如果指定的標頭已存在, value則會新增至與標頭相關聯之值的逗號分隔清單。If the specified header is already present, value is added to the comma-separated list of values associated with the header.

Add(HttpResponseHeader, String)

將具有指定值的指定標頭插入至集合。Inserts the specified header with the specified value into the collection.

public:
 void Add(System::Net::HttpResponseHeader header, System::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

要加入至集合的標頭。The header to add to the collection.

value
String

標頭的內容。The content of the header.

例外狀況

value 的長度大於 65535。The length of value is greater than 65535.

這個 WebHeaderCollection 執行個體不允許 HttpResponseHeader 的執行個體。This WebHeaderCollection instance does not allow instances of HttpResponseHeader.

備註

如果指定的標頭不存在,則Add方法會將新的標頭插入標頭名稱/值組的清單中。If the specified header does not exist, the Add method inserts a new header into the list of header name/value pairs.

如果指定的標頭已存在, value則會新增至與標頭相關聯之值的逗號分隔清單。If the specified header is already present, value is added to the comma-separated list of values associated with the header.

Add(String, String)

將具有指定名稱和值的標頭插入至集合。Inserts a header with the specified name and value into the collection.

public:
 override void Add(System::String ^ name, System::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

要加入至集合的標頭。The header to add to the collection.

value
String

標頭的內容。The content of the header.

例外狀況

namenullEmpty 或含有無效字元。name is null, Empty, or contains invalid characters.

-或--or- name 是必須使用屬性設定值加以設定之受限制的標頭。name is a restricted header that must be set with a property setting.

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

value 的長度大於 65535。The length of value is greater than 65535.

範例

下列範例會WebHeaderCollection Add使用方法,將名稱/值組加入至。The following example adds a name/value pair to a WebHeaderCollection using the Add Method.

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

備註

如果中name指定的標頭不存在,則Add方法會將新的標頭插入標頭名稱/值組的清單中。If the header specified in name does not exist, the Add method inserts a new header into the list of header name/value pairs.

如果中指定的標name頭已存在, value則會加入至與name相關聯之值的現有逗號分隔清單中。If the header specified in name is already present, value is added to the existing comma-separated list of values associated with name.

適用於