WebHeaderCollection.Add Metodo

Definizione

Inserisce una nuova intestazione nell'insieme.

Overload

Add(String)

Inserisce nella raccolta l'intestazione specificata.

Add(HttpRequestHeader, String)

Inserisce nella raccolta l'intestazione specificata con il valore specificato.

Add(HttpResponseHeader, String)

Inserisce nella raccolta l'intestazione specificata con il valore specificato.

Add(String, String)

Inserisce nella raccolta un'intestazione con il nome e il valore specificati.

Add(String)

Origine:
WebHeaderCollection.cs
Origine:
WebHeaderCollection.cs
Origine:
WebHeaderCollection.cs

Inserisce nella raccolta l'intestazione specificata.

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

Parametri

header
String

L'intestazione da aggiungere, con il nome e il valore separati dal carattere dei due punti (:).

Eccezioni

header è null o Empty.

header non contiene un carattere di due punti (:).

La lunghezza di value è maggiore di 65535.

-oppure-

La parte relativa al nome di header è Empty o contiene caratteri non validi.

-oppure-

header è un'intestazione con restrizioni che deve essere impostata con una proprietà.

-oppure-

La parte relativa al valore di header contiene caratteri non validi.

Solo .NET Framework e .NET Core versioni 2.0 - 3.1: la lunghezza della stringa dopo i due punti (:) è maggiore di 65535 caratteri.

Esempio

Nell'esempio seguente viene aggiunta una coppia nome/valore a un WebHeaderCollection oggetto utilizzando il Add metodo .

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

Nota

La lunghezza della parte del valore di header, ovvero la stringa dopo i due punti (:), viene convalidata solo in .NET Framework e .NET Core versioni 2.0 - 3.1.

Commenti

Il header parametro deve essere specificato nel formato "name:value". Se l'intestazione specificata non esiste nella raccolta, viene aggiunta una nuova intestazione alla raccolta.

Se l'intestazione specificata in header è già presente nella raccolta, la parte del valore di header viene concatenata con il valore esistente.

Si applica a

Add(HttpRequestHeader, String)

Origine:
WebHeaderCollection.cs
Origine:
WebHeaderCollection.cs
Origine:
WebHeaderCollection.cs

Inserisce nella raccolta l'intestazione specificata con il valore specificato.

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)

Parametri

header
HttpRequestHeader

Intestazione da aggiungere alla raccolta.

value
String

Contenuto dell'intestazione.

Eccezioni

Solo .NET Framework e .NET Core versioni 2.0 - 3.1: la lunghezza di value è maggiore di 65535 caratteri.

Questa istanza WebHeaderCollection non consente istanze di HttpRequestHeader.

Commenti

Se l'intestazione specificata non esiste, il Add metodo inserisce una nuova intestazione nell'elenco di coppie nome/valore dell'intestazione.

Se l'intestazione specificata è già presente, value viene aggiunta all'elenco delimitato da virgole di valori associati all'intestazione.

Nota

La lunghezza di value viene convalidata solo in .NET Framework e .NET Core versioni 2.0 - 3.1.

Si applica a

Add(HttpResponseHeader, String)

Origine:
WebHeaderCollection.cs
Origine:
WebHeaderCollection.cs
Origine:
WebHeaderCollection.cs

Inserisce nella raccolta l'intestazione specificata con il valore specificato.

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)

Parametri

header
HttpResponseHeader

Intestazione da aggiungere alla raccolta.

value
String

Contenuto dell'intestazione.

Eccezioni

Solo .NET Framework e .NET Core versioni 2.0 - 3.1: la lunghezza di value è maggiore di 65535 caratteri.

Questa istanza WebHeaderCollection non consente istanze di HttpResponseHeader.

Commenti

Se l'intestazione specificata non esiste, il Add metodo inserisce una nuova intestazione nell'elenco di coppie nome/valore dell'intestazione.

Se l'intestazione specificata è già presente, value viene aggiunta all'elenco delimitato da virgole di valori associati all'intestazione.

Nota

La lunghezza di value viene convalidata solo in .NET Framework e .NET Core versioni 2.0 - 3.1.

Si applica a

Add(String, String)

Origine:
WebHeaderCollection.cs
Origine:
WebHeaderCollection.cs
Origine:
WebHeaderCollection.cs

Inserisce nella raccolta un'intestazione con il nome e il valore specificati.

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)

Parametri

name
String

Intestazione da aggiungere alla raccolta.

value
String

Contenuto dell'intestazione.

Eccezioni

name è null, Empty oppure contiene caratteri non validi.

-oppure-

name è un'intestazione con restrizioni da configurare con un'impostazione delle proprietà.

-oppure-

value contiene caratteri non validi.

Solo .NET Framework e .NET Core versioni 2.0 - 3.1: la lunghezza di value è maggiore di 65535 caratteri.

Esempio

Nell'esempio seguente viene aggiunta una coppia nome/valore a un WebHeaderCollection oggetto utilizzando il Add metodo .

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

Nota

La lunghezza di value viene convalidata solo in .NET Framework e .NET Core versioni 2.0 - 3.1.

Commenti

Se l'intestazione specificata in name non esiste, il Add metodo inserisce una nuova intestazione nell'elenco di coppie nome/valore dell'intestazione.

Se l'intestazione specificata in name è già presente, value viene aggiunta all'elenco delimitato da virgole esistente di valori associati a name.

Si applica a