Condividi tramite


WebClient.Headers Proprietà

Definizione

Ottiene o imposta una raccolta di coppie nome/valore di intestazione associate alla richiesta.

public:
 property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); void set(System::Net::WebHeaderCollection ^ value); };
public System.Net.WebHeaderCollection Headers { get; set; }
member this.Headers : System.Net.WebHeaderCollection with get, set
Public Property Headers As WebHeaderCollection

Valore della proprietà

Un WebHeaderCollection contenente coppie nome/valore di intestazione associate a questa richiesta.

Esempio

Nell'esempio di codice seguente viene utilizzata la raccolta Headers per impostare l'intestazione HTTP Content-Type su application/x-www-form-urlencoded, per notificare al server che i dati del modulo sono collegati al post.

String^ uriString;
Console::Write( "\nPlease enter the URI to post data to {for example, http://www.contoso.com}: " );
uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine( "\nPlease enter the data to be posted to the URI {0}:", uriString );
String^ postData = Console::ReadLine();
myWebClient->Headers->Add( "Content-Type", "application/x-www-form-urlencoded" );

// Displays the headers in the request
Console::Write( "Resulting Request Headers: ");
Console::WriteLine(myWebClient->Headers);

// Apply ASCII Encoding to obtain the String^ as a Byte array.
array<Byte>^ byteArray = Encoding::ASCII->GetBytes( postData );
Console::WriteLine( "Uploading to {0} ...", uriString );
// Upload the input String* using the HTTP 1.0 POST method.
array<Byte>^responseArray = myWebClient->UploadData( uriString, "POST", byteArray );
// Decode and display the response.
Console::WriteLine( "\nResponse received was {0}",
   Encoding::ASCII->GetString( responseArray ) );
   string uriString;
       Console.Write("\nPlease enter the URI to post data to {for example, http://www.contoso.com} : ");
       uriString = Console.ReadLine();

       // Create a new WebClient instance.
       WebClient myWebClient = new WebClient();
       Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
       string postData = Console.ReadLine();
       myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

 // Display the headers in the request
       Console.Write("Resulting Request Headers: ");
       Console.WriteLine(myWebClient.Headers.ToString());
       
       // Apply ASCII Encoding to obtain the string as a byte array.

       byte[] byteArray = Encoding.ASCII.GetBytes(postData);
       Console.WriteLine("Uploading to {0} ...",  uriString);						
       // Upload the input string using the HTTP 1.0 POST method.
       byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
       
       // Decode and display the response.
       Console.WriteLine("\nResponse received was {0}",
       Encoding.ASCII.GetString(responseArray));
                 
Dim uriString As String
Console.Write(ControlChars.Cr + "Please enter the URI to post data to{for example, http://www.contoso.com} : ")
uriString = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()
Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the URI {0}:", uriString)
Dim postData As String = Console.ReadLine()
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

' Display the headers in the request
Console.Write("Resulting Request Headers: ")
Console.Writeline(myWebClient.Headers.ToString())

' Apply ASCII Encoding to obtain the string as a byte array.
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uriString)
' Upload the input string using the HTTP 1.0 POST method.
Dim responseArray As Byte() = myWebClient.UploadData(uriString, "POST", byteArray)
' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :{0}", Encoding.ASCII.GetString(responseArray))

Commenti

Cautela

WebRequest, HttpWebRequest, ServicePointe WebClient sono obsoleti e non è consigliabile usarli per nuovi sviluppi. Usare invece HttpClient.

La proprietà Headers contiene un'istanza di WebHeaderCollection contenente le intestazioni di protocollo inviate dal WebClient con la richiesta.

Alcune intestazioni comuni sono considerate limitate e sono protette dal sistema e non possono essere impostate o modificate in un oggetto WebHeaderCollection. Qualsiasi tentativo di impostare una di queste intestazioni limitate nell'oggetto WebHeaderCollection associato a un oggetto WebClient genererà un'eccezione in un secondo momento quando si tenta di inviare la richiesta di WebClient.

Le intestazioni con restrizioni protette dal sistema includono, ma non sono limitate alle seguenti:

  • Dattero

  • Ospite

Inoltre, alcune altre intestazioni sono limitate quando si usa un oggetto WebClient. Queste intestazioni con restrizioni includono, ma non sono limitate alle seguenti:

  • Accettare

  • Connessione

  • Lunghezza contenuto

  • Previsto (quando il valore è impostato su "100-continue"

  • If-Modified-Since

  • Gamma

  • Transfer-Encoding

La classe HttpWebRequest ha proprietà per l'impostazione di alcune delle intestazioni precedenti. Se è importante che un'applicazione imposti queste intestazioni, la classe HttpWebRequest deve essere usata invece della classe WebRequest.

Non è consigliabile presupporre che i valori dell'intestazione rimangano invariati, perché i server Web e le cache possono modificare o aggiungere intestazioni a una richiesta Web.

Si applica a

Vedi anche