Condividi tramite


WebHeaderCollection.Add Metodo

Definizione

Inserisce una nuova intestazione nella raccolta.

Overload

Nome Descrizione
Add(String)

Inserisce l'intestazione specificata nella raccolta.

Add(HttpRequestHeader, String)

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

Add(HttpResponseHeader, String)

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

Add(String, String)

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

Add(String)

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

Inserisce l'intestazione specificata nella raccolta.

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

Intestazione da aggiungere, con il nome e il valore separati da due punti.

Eccezioni

header è null o Empty.

header non contiene due punti (:) carattere.

oppure

La lunghezza della parte del valore di header è maggiore di 65535.

oppure

La parte del 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 del valore di header contiene caratteri non validi.

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

Esempio

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

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

Annotazioni

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 nell'insieme, 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
Origine:
WebHeaderCollection.cs
Origine:
WebHeaderCollection.cs

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

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 è maggiore di value 65535.

Questa WebHeaderCollection istanza 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.

Annotazioni

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

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

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 è maggiore di value 65535.

Questa WebHeaderCollection istanza 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.

Annotazioni

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

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

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, Emptyo contiene caratteri non validi.

oppure

name è un'intestazione con restrizioni che deve essere impostata con un'impostazione di proprietà.

oppure

value contiene caratteri non validi.

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

Esempio

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

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

Annotazioni

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 di 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