WebHeaderCollection.Add Metoda

Definicja

Wstawia nowy nagłówek do kolekcji.

Przeciążenia

Add(String)

Wstawia określony nagłówek do kolekcji.

Add(HttpRequestHeader, String)

Wstawia określony nagłówek z określoną wartością do kolekcji.

Add(HttpResponseHeader, String)

Wstawia określony nagłówek z określoną wartością do kolekcji.

Add(String, String)

Wstawia nagłówek o określonej nazwie i wartości do kolekcji.

Add(String)

Wstawia określony nagłówek do kolekcji.

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

Parametry

header
String

Nagłówek do dodania z nazwą i wartością rozdzielaną dwukropkiem.

Wyjątki

header to null lub Empty.

header nie zawiera dwukropka (:) Znaków.

Długość value jest większa niż 65535.

-lub-

Część nazwy elementu header jest Empty lub zawiera nieprawidłowe znaki.

-lub-

header jest nagłówkiem z ograniczeniami, który należy ustawić z właściwością.

-lub-

Część wartości zawiera header nieprawidłowe znaki.

tylko .NET Framework i .NET Core w wersji 2.0–3.1: długość ciągu po dwukropku (:) jest większy niż 65535.

Przykłady

Poniższy przykład dodaje parę nazwa/wartość do klasy WebHeaderCollection przy użyciu Add metody .

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

Uwaga

Długość części headerwartości , czyli ciąg po dwukropku (:), jest weryfikowany tylko w .NET Framework i .NET Core w wersji 2.0 –3.1.

Uwagi

Parametr header musi być określony w formacie "name:value". Jeśli określony nagłówek nie istnieje w kolekcji, do kolekcji zostanie dodany nowy nagłówek.

Jeśli nagłówek określony w header pliku jest już obecny w kolekcji, część header wartości jest połączona z istniejącą wartością.

Dotyczy

Add(HttpRequestHeader, String)

Wstawia określony nagłówek z określoną wartością do kolekcji.

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)

Parametry

header
HttpRequestHeader

Nagłówek do dodania do kolekcji.

value
String

Zawartość nagłówka.

Wyjątki

.NET Framework i .NET Core w wersji 2.0 – 3.1: długość value jest większa niż 65535.

To WebHeaderCollection wystąpienie nie zezwala na wystąpienia programu HttpRequestHeader.

Uwagi

Jeśli określony nagłówek nie istnieje, Add metoda wstawia nowy nagłówek do listy par nazwa/wartość nagłówka.

Jeśli określony nagłówek jest już obecny, value zostanie dodany do rozdzielanej przecinkami listy wartości skojarzonych z nagłówkiem.

Uwaga

Długość value wartości jest weryfikowana tylko w .NET Framework i .NET Core w wersji 2.0–3.1.

Dotyczy

Add(HttpResponseHeader, String)

Wstawia określony nagłówek z określoną wartością do kolekcji.

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)

Parametry

header
HttpResponseHeader

Nagłówek do dodania do kolekcji.

value
String

Zawartość nagłówka.

Wyjątki

.NET Framework i .NET Core w wersji 2.0 – 3.1: długość value jest większa niż 65535.

To WebHeaderCollection wystąpienie nie zezwala na wystąpienia programu HttpResponseHeader.

Uwagi

Jeśli określony nagłówek nie istnieje, Add metoda wstawia nowy nagłówek do listy par nazwa/wartość nagłówka.

Jeśli określony nagłówek jest już obecny, value zostanie dodany do rozdzielanej przecinkami listy wartości skojarzonych z nagłówkiem.

Uwaga

Długość value wartości jest weryfikowana tylko w .NET Framework i .NET Core w wersji 2.0–3.1.

Dotyczy

Add(String, String)

Wstawia nagłówek o określonej nazwie i wartości do kolekcji.

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)

Parametry

name
String

Nagłówek do dodania do kolekcji.

value
String

Zawartość nagłówka.

Wyjątki

name to null, Emptylub zawiera nieprawidłowe znaki.

-lub-

name jest nagłówkiem z ograniczeniami, który należy ustawić z ustawieniem właściwości.

-lub-

value zawiera nieprawidłowe znaki.

.NET Framework i .NET Core w wersji 2.0 – 3.1: długość value jest większa niż 65535.

Przykłady

Poniższy przykład dodaje parę nazwa/wartość do klasy WebHeaderCollection przy użyciu Add metody .

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

Uwaga

Długość value wartości jest weryfikowana tylko w .NET Framework i .NET Core w wersji 2.0–3.1.

Uwagi

Jeśli nagłówek określony w name pliku nie istnieje, Add metoda wstawia nowy nagłówek do listy par nazwa/wartość nagłówka.

Jeśli nagłówek określony w pliku name jest już obecny, value zostanie dodany do istniejącej listy wartości rozdzielonych przecinkami skojarzonych z name.

Dotyczy