WebHeaderCollection.Set Método

Definición

Establece el encabezado especificado en el valor indicado.Sets the specified header to the specified value.

Sobrecargas

Set(HttpRequestHeader, String)

Establece el encabezado especificado en el valor indicado.Sets the specified header to the specified value.

Set(HttpResponseHeader, String)

Establece el encabezado especificado en el valor indicado.Sets the specified header to the specified value.

Set(String, String)

Establece el encabezado especificado en el valor indicado.Sets the specified header to the specified value.

Set(HttpRequestHeader, String)

Establece el encabezado especificado en el valor indicado.Sets the specified header to the specified value.

public:
 void Set(System::Net::HttpRequestHeader header, System::String ^ value);
public void Set (System.Net.HttpRequestHeader header, string? value);
public void Set (System.Net.HttpRequestHeader header, string value);
override this.Set : System.Net.HttpRequestHeader * string -> unit
Public Sub Set (header As HttpRequestHeader, value As String)

Parámetros

header
HttpRequestHeader

Valor de HttpRequestHeader que se va a establecer.The HttpRequestHeader value to set.

value
String

Contenido del encabezado que se va a establecer.The content of the header to set.

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 no existe el encabezado especificado en el encabezado, el Set método inserta un nuevo encabezado en la lista de pares de nombre y valor de encabezado.If the header specified in the header does not exist, the Set method inserts a new header into the list of header name/value pairs.

Si el encabezado especificado en header ya está presente, value reemplaza el valor existente.If the header specified in header is already present, value replaces the existing value.

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

Set(HttpResponseHeader, String)

Establece el encabezado especificado en el valor indicado.Sets the specified header to the specified value.

public:
 void Set(System::Net::HttpResponseHeader header, System::String ^ value);
public void Set (System.Net.HttpResponseHeader header, string? value);
public void Set (System.Net.HttpResponseHeader header, string value);
override this.Set : System.Net.HttpResponseHeader * string -> unit
Public Sub Set (header As HttpResponseHeader, value As String)

Parámetros

header
HttpResponseHeader

Valor de HttpResponseHeader que se va a establecer.The HttpResponseHeader value to set.

value
String

Contenido del encabezado que se va a establecer.The content of the header to set.

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 no existe el encabezado especificado en el encabezado, el Set método inserta un nuevo encabezado en la lista de pares de nombre y valor de encabezado.If the header specified in the header does not exist, the Set method inserts a new header into the list of header name/value pairs.

Si el encabezado especificado en header ya está presente, value reemplaza el valor existente.If the header specified in header is already present, value replaces the existing value.

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

Set(String, String)

Establece el encabezado especificado en el valor indicado.Sets the specified header to the specified value.

public:
 override void Set(System::String ^ name, System::String ^ value);
public override void Set (string name, string? value);
public override void Set (string name, string value);
override this.Set : string * string -> unit
Public Overrides Sub Set (name As String, value As String)

Parámetros

name
String

Encabezado que se va a establecer.The header to set.

value
String

Contenido del encabezado que se va a establecer.The content of the header to set.

Excepciones

name es null o Empty.name is null or Empty.

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

name es un encabezado restringido.name is a restricted header.

o bien-or- name o value contienen caracteres no válidos.name or value contain invalid characters.

Ejemplos

En el ejemplo siguiente se usa el Set método para establecer el valor de un encabezado existente.The following example uses the Set method to set the value of an existing header.

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;

   // Set the Cache-Control header in the request.
   myWebHeaderCollection->Set( "Cache-Control", "no-cache" );

   // Get the associated response for the above request.
   HttpWebResponse^ myHttpWebResponse = dynamic_cast<HttpWebResponse^>(myHttpWebRequest->GetResponse());
   Console::WriteLine( "Headers after 'Set' method is used on Cache-Control :" );

   // Print the headers for the request.
   PrintHeaders( myWebHeaderCollection );
   myHttpWebResponse->Close();
}
// Catch exception if trying to set a restricted header.
catch ( ArgumentException^ e ) 
{
   Console::WriteLine( "ArgumentException is thrown. Message is : {0}", e->Message );
}
catch ( WebException^ e ) 
{
   Console::WriteLine( "WebException is thrown. Message 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;

    // Set the Cache-Control header in the request.
    myWebHeaderCollection.Set("Cache-Control", "no-cache");

    // Get the associated response for the above request.
    HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();

    Console.WriteLine ("Headers after 'Set' method is used on Cache-Control :");
    // Print the headers for the request.
    PrintHeaders(myWebHeaderCollection);
    myHttpWebResponse.Close();
}
// Catch exception if trying to set a restricted header.
catch(ArgumentException e) {
    Console.WriteLine("ArgumentException is thrown. Message is :" + e.Message);
}
catch(WebException e) {
    Console.WriteLine("WebException is thrown. Message 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
        
        'Set the Cache-Control header in the request.
        myWebHeaderCollection.Set("Cache-Control", "no-cache")

        'Get the associated response for the above request.
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        
    Console.WriteLine ("Headers after 'Set' method is used on Cache-Control :")
        'Print the headers for the request.
        PrintHeaders(myWebHeaderCollection)
        myHttpWebResponse.Close()
   'Catch exception if trying to set 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 no existe el encabezado especificado en el encabezado, el Set método inserta un nuevo encabezado en la lista de pares de nombre y valor de encabezado.If the header specified in the header does not exist, the Set method inserts a new header into the list of header name/value pairs.

Si el encabezado especificado en header ya está presente, value reemplaza el valor existente.If the header specified in header is already present, value replaces the existing value.

Se aplica a