WebHeaderCollection.Add Metoda

Definice

Vloží do kolekce nové záhlaví.

Přetížení

Add(String)

Vloží zadanou hlavičku do kolekce.

Add(HttpRequestHeader, String)

Vloží zadanou hlavičku se zadanou hodnotou do kolekce.

Add(HttpResponseHeader, String)

Vloží zadanou hlavičku se zadanou hodnotou do kolekce.

Add(String, String)

Vloží do kolekce hlavičku se zadaným názvem a hodnotou.

Add(String)

Zdroj:
WebHeaderCollection.cs
Zdroj:
WebHeaderCollection.cs
Zdroj:
WebHeaderCollection.cs

Vloží zadanou hlavičku do kolekce.

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

Záhlaví, které se má přidat, s názvem a hodnotou oddělenou dvojtečku.

Výjimky

header je null nebo Empty.

header neobsahuje dvojtečku (:) Znak.

Délka value je větší než 65535.

-nebo-

Část názvu header je Empty nebo obsahuje neplatné znaky.

-nebo-

header je omezená hlavička, která by měla být nastavena vlastností.

-nebo-

Část header hodnoty obsahuje neplatné znaky.

Pouze .NET Framework a .NET Core verze 2.0–3.1: Délka řetězce za dvojtečku (:) je větší než 65535.

Příklady

Následující příklad přidá dvojici název/hodnota pomocí WebHeaderCollectionAdd 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

Poznámka

Délka hodnotové části header, tj. řetězec za dvojtečku (:), se ověřuje pouze v rozhraní .NET Framework a .NET Core verze 2.0 až 3.1.

Poznámky

Parametr header musí být zadán ve formátu "name:value". Pokud zadaná hlavička v kolekci neexistuje, přidá se do kolekce nová hlavička.

Pokud je hlavička zadaná v header již v kolekci, hodnota část header je zřetězena s existující hodnotou.

Platí pro

Add(HttpRequestHeader, String)

Zdroj:
WebHeaderCollection.cs
Zdroj:
WebHeaderCollection.cs
Zdroj:
WebHeaderCollection.cs

Vloží zadanou hlavičku se zadanou hodnotou do kolekce.

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

Záhlaví, které se má přidat do kolekce.

value
String

Obsah záhlaví.

Výjimky

Pouze .NET Framework a .NET Core verze 2.0 až 3.1: Délka value je větší než 65535.

Poznámky

Pokud zadaná hlavička neexistuje, Add metoda vloží nové záhlaví do seznamu dvojic název/hodnota záhlaví.

Pokud zadaná hlavička již existuje, value přidá se do seznamu hodnot oddělených čárkami přidružených k záhlaví.

Poznámka

Délka value se ověřuje pouze v rozhraní .NET Framework a .NET Core verze 2.0 až 3.1.

Platí pro

Add(HttpResponseHeader, String)

Zdroj:
WebHeaderCollection.cs
Zdroj:
WebHeaderCollection.cs
Zdroj:
WebHeaderCollection.cs

Vloží zadanou hlavičku se zadanou hodnotou do kolekce.

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

Záhlaví, které se má přidat do kolekce.

value
String

Obsah záhlaví.

Výjimky

Pouze .NET Framework a .NET Core verze 2.0 až 3.1: Délka value je větší než 65535.

Poznámky

Pokud zadaná hlavička neexistuje, Add metoda vloží nové záhlaví do seznamu dvojic název/hodnota záhlaví.

Pokud zadaná hlavička již existuje, value přidá se do seznamu hodnot oddělených čárkami přidružených k záhlaví.

Poznámka

Délka value se ověřuje pouze v rozhraní .NET Framework a .NET Core verze 2.0 až 3.1.

Platí pro

Add(String, String)

Zdroj:
WebHeaderCollection.cs
Zdroj:
WebHeaderCollection.cs
Zdroj:
WebHeaderCollection.cs

Vloží do kolekce hlavičku se zadaným názvem a hodnotou.

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

Záhlaví, které se má přidat do kolekce.

value
String

Obsah záhlaví.

Výjimky

name je null, Emptynebo obsahuje neplatné znaky.

-nebo-

name je omezená hlavička, která musí být nastavena nastavením vlastnosti.

-nebo-

value obsahuje neplatné znaky.

Pouze .NET Framework a .NET Core verze 2.0 až 3.1: Délka value je větší než 65535.

Příklady

Následující příklad přidá dvojici název/hodnota pomocí WebHeaderCollectionAdd 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

Poznámka

Délka value se ověřuje pouze v rozhraní .NET Framework a .NET Core verze 2.0 až 3.1.

Poznámky

Pokud zadaná hlavička neexistuje name , Add metoda vloží nové záhlaví do seznamu dvojic název/hodnota záhlaví.

Pokud hlavička zadaná v name souboru již existuje, value přidá se do existujícího seznamu hodnot přidružených namek oddělenému čárkami.

Platí pro