WebHeaderCollection.Set Método

Definição

Define o cabeçalho especificado para o valor especificado.

Sobrecargas

Set(HttpRequestHeader, String)

Define o cabeçalho especificado para o valor especificado.

Set(HttpResponseHeader, String)

Define o cabeçalho especificado para o valor especificado.

Set(String, String)

Define o cabeçalho especificado para o valor especificado.

Set(HttpRequestHeader, String)

Origem:
WebHeaderCollection.cs
Origem:
WebHeaderCollection.cs
Origem:
WebHeaderCollection.cs

Define o cabeçalho especificado para o valor especificado.

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

O valor HttpRequestHeader a ser definido.

value
String

O conteúdo do cabeçalho a ser definido.

Exceções

.NET Framework e NET Core versões 2.0 - 3.1 apenas: o comprimento de value é maior que 65.535.

Essa instância WebHeaderCollection não permite instâncias de HttpRequestHeader.

Comentários

Se o cabeçalho especificado no cabeçalho não existir, o Set método inserirá um novo cabeçalho na lista de pares nome/valor do cabeçalho.

Se o cabeçalho especificado em header já estiver presente, value substituirá o valor existente.

Observação

O comprimento de value é validado apenas nas versões 2.0 e 3.1 do .NET Framework e do .NET Core.

Aplica-se a

Set(HttpResponseHeader, String)

Origem:
WebHeaderCollection.cs
Origem:
WebHeaderCollection.cs
Origem:
WebHeaderCollection.cs

Define o cabeçalho especificado para o valor especificado.

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

O valor HttpResponseHeader a ser definido.

value
String

O conteúdo do cabeçalho a ser definido.

Exceções

.NET Framework e NET Core versões 2.0 - 3.1 apenas: o comprimento de value é maior que 65.535.

Essa instância WebHeaderCollection não permite instâncias de HttpResponseHeader.

Comentários

Se o cabeçalho especificado no cabeçalho não existir, o Set método inserirá um novo cabeçalho na lista de pares nome/valor do cabeçalho.

Se o cabeçalho especificado em header já estiver presente, value substituirá o valor existente.

Observação

O comprimento de value é validado apenas nas versões 2.0 e 3.1 do .NET Framework e do .NET Core.

Aplica-se a

Set(String, String)

Origem:
WebHeaderCollection.cs
Origem:
WebHeaderCollection.cs
Origem:
WebHeaderCollection.cs

Define o cabeçalho especificado para o valor especificado.

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

O cabeçalho a definir.

value
String

O conteúdo do cabeçalho a ser definido.

Exceções

name é null ou Empty.

.NET Framework e NET Core versões 2.0 - 3.1 apenas: o comprimento de value é maior que 65.535.

name é um cabeçalho restrito.

- ou -

name ou value contêm caracteres inválidos.

Exemplos

O exemplo a seguir usa o Set método para definir o valor de um cabeçalho existente.

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

Observação

O comprimento de value é validado apenas nas versões 2.0 e 3.1 do .NET Framework e do .NET Core.

Comentários

Se o cabeçalho especificado no cabeçalho não existir, o Set método inserirá um novo cabeçalho na lista de pares nome/valor do cabeçalho.

Se o cabeçalho especificado em header já estiver presente, value substituirá o valor existente.

Aplica-se a