WebHeaderCollection.Add Método

Definición

Inserta un nuevo encabezado en la colección.Inserts a new header into the collection.

Sobrecargas

Add(String)

Inserta el encabezado especificado en la colección.Inserts the specified header into the collection.

Add(HttpRequestHeader, String)

Inserta el encabezado especificado con el valor especificado en la colección.Inserts the specified header with the specified value into the collection.

Add(HttpResponseHeader, String)

Inserta el encabezado especificado con el valor especificado en la colección.Inserts the specified header with the specified value into the collection.

Add(String, String)

Inserta en la colección un encabezado con el nombre y valor especificados.Inserts a header with the specified name and value into the collection.

Add(String)

Inserta el encabezado especificado en la colección.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)

Parámetros

header
String

Encabezado que se va a agregar, con el nombre y el valor separados por dos puntos.The header to add, with the name and value separated by a colon.

Excepciones

header es null o Empty.header is null or Empty.

header no contiene un carácter de dos puntos (:).header does not contain a colon (:) character.

La longitud de value es mayor que 65535.The length of value is greater than 65535.

o bien-or- La parte correspondiente al nombre de header es Empty o contiene caracteres no válidos.The name part of header is Empty or contains invalid characters.

o bien-or- header es un encabezado restringido que debe establecerse con una propiedad.header is a restricted header that should be set with a property.

o bien-or- La parte correspondiente al valor de header contiene caracteres no válidos.The value part of header contains invalid characters.

.NET Framework y versiones 2.0-3.1 de .NET Core únicamente: la longitud de la cadena después de los dos puntos (:) es mayor de 65 535..NET Framework and .NET Core versions 2.0 - 3.1 only: The length of the string after the colon (:) is greater than 65535.

Ejemplos

En el siguiente ejemplo se agrega un par de nombre y valor a un WebHeaderCollection mediante el Add método.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

Nota

La longitud de la parte del valor de header , es decir, la cadena situada detrás del signo de dos puntos (:), solo se valida en .NET Framework y en las versiones de .net Core 2,0-3,1.The length of the value portion of header, that is, the string after the colon (:), is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.

Comentarios

El header parámetro debe especificarse con el formato "nombre: valor".The header parameter must be specified in the format "name:value". Si el encabezado especificado no existe en la colección, se agrega un nuevo encabezado a la colección.If the specified header does not exist in the collection, a new header is added to the collection.

Si el encabezado especificado en header ya está presente en la colección, la parte del valor de header se concatena con el valor existente.If the header specified in header is already present in the collection, the value part of the header is concatenated with the existing value.

Se aplica a

Add(HttpRequestHeader, String)

Inserta el encabezado especificado con el valor especificado en la colección.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);
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)

Parámetros

header
HttpRequestHeader

Encabezado que se agrega a la colección.The header to add to the collection.

value
String

El contenido del encabezado.The content of the header.

Excepciones

.NET Framework y versiones 2.0-3.1 de .NET Core únicamente: la longitud de value es mayor de 65 535..NET Framework and .NET Core versions 2.0 - 3.1 only: The length of value is greater than 65535.

Esta instancia de WebHeaderCollection no permite instancias de HttpRequestHeader.This WebHeaderCollection instance does not allow instances of HttpRequestHeader.

Comentarios

Si el encabezado especificado no existe, el Add método inserta un nuevo encabezado en la lista de pares de nombre y valor de encabezado.If the specified header does not exist, the Add method inserts a new header into the list of header name/value pairs.

Si el encabezado especificado ya está presente, value se agrega a la lista de valores separados por comas asociada al encabezado.If the specified header is already present, value is added to the comma-separated list of values associated with the header.

Nota

La longitud de value solo se valida en .NET Framework y en las versiones de .net Core 2,0-3,1.The length of value is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.

Se aplica a

Add(HttpResponseHeader, String)

Inserta el encabezado especificado con el valor especificado en la colección.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);
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)

Parámetros

header
HttpResponseHeader

Encabezado que se agrega a la colección.The header to add to the collection.

value
String

El contenido del encabezado.The content of the header.

Excepciones

.NET Framework y versiones 2.0-3.1 de .NET Core únicamente: la longitud de value es mayor de 65 535..NET Framework and .NET Core versions 2.0 - 3.1 only: The length of value is greater than 65535.

Esta instancia de WebHeaderCollection no permite instancias de HttpResponseHeader.This WebHeaderCollection instance does not allow instances of HttpResponseHeader.

Comentarios

Si el encabezado especificado no existe, el Add método inserta un nuevo encabezado en la lista de pares de nombre y valor de encabezado.If the specified header does not exist, the Add method inserts a new header into the list of header name/value pairs.

Si el encabezado especificado ya está presente, value se agrega a la lista de valores separados por comas asociada al encabezado.If the specified header is already present, value is added to the comma-separated list of values associated with the header.

Nota

La longitud de value solo se valida en .NET Framework y en las versiones de .net Core 2,0-3,1.The length of value is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.

Se aplica a

Add(String, String)

Inserta en la colección un encabezado con el nombre y valor especificados.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);
public override void Add (string name, string value);
override this.Add : string * string -> unit
Public Overrides Sub Add (name As String, value As String)

Parámetros

name
String

Encabezado que se agrega a la colección.The header to add to the collection.

value
String

El contenido del encabezado.The content of the header.

Excepciones

name es null, Empty o contiene caracteres no válidos.name is null, Empty, or contains invalid characters.

o bien-or- name es un encabezado restringido que debe establecerse con un valor de propiedad.name is a restricted header that must be set with a property setting.

o bien-or- value contiene caracteres no válidos.value contains invalid characters.

.NET Framework y versiones 2.0-3.1 de .NET Core únicamente: la longitud de value es mayor de 65 535..NET Framework and .NET Core versions 2.0 - 3.1 only: The length of value is greater than 65535.

Ejemplos

En el siguiente ejemplo se agrega un par de nombre y valor a un WebHeaderCollection mediante el Add método.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

Nota

La longitud de value solo se valida en .NET Framework y en las versiones de .net Core 2,0-3,1.The length of value is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.

Comentarios

Si el encabezado especificado en name no existe, el Add método inserta un nuevo encabezado en la lista de pares de nombre y valor de encabezado.If the header specified in name does not exist, the Add method inserts a new header into the list of header name/value pairs.

Si el encabezado especificado en name ya está presente, value se agrega a la lista de valores separados por comas existente asociada a name .If the header specified in name is already present, value is added to the existing comma-separated list of values associated with name.

Se aplica a