WebHeaderCollection.Set Metodo

Definizione

Imposta l'intestazione specificata sul valore specificato.

Overload

Set(HttpRequestHeader, String)

Imposta l'intestazione specificata sul valore specificato.

Set(HttpResponseHeader, String)

Imposta l'intestazione specificata sul valore specificato.

Set(String, String)

Imposta l'intestazione specificata sul valore specificato.

Set(HttpRequestHeader, String)

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

Imposta l'intestazione specificata sul valore specificato.

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)

Parametri

header
HttpRequestHeader

Valore HttpRequestHeader da impostare.

value
String

Il contenuto dell'intestazione da impostare.

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 nell'intestazione non esiste, il Set metodo inserisce una nuova intestazione nell'elenco di coppie nome/valore di intestazione.

Se l'intestazione specificata in header è già presente, value sostituisce il valore esistente.

Nota

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

Si applica a

Set(HttpResponseHeader, String)

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

Imposta l'intestazione specificata sul valore specificato.

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)

Parametri

header
HttpResponseHeader

Valore HttpResponseHeader da impostare.

value
String

Il contenuto dell'intestazione da impostare.

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 nell'intestazione non esiste, il Set metodo inserisce una nuova intestazione nell'elenco di coppie nome/valore di intestazione.

Se l'intestazione specificata in header è già presente, value sostituisce il valore esistente.

Nota

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

Si applica a

Set(String, String)

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

Imposta l'intestazione specificata sul valore specificato.

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)

Parametri

name
String

L'intestazione da impostare.

value
String

Il contenuto dell'intestazione da impostare.

Eccezioni

name è null o Empty.

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

name è un'intestazione con restrizioni.

-oppure-

name o value contengono caratteri non validi.

Esempio

Nell'esempio seguente viene usato il metodo per impostare il Set valore di un'intestazione esistente.

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 lunghezza di value viene convalidata solo in .NET Framework e in .NET Core versioni 2.0 - 3.1.

Commenti

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

Se l'intestazione specificata in header è già presente, value sostituisce il valore esistente.

Si applica a