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à

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

Esempio

Nell'esempio di codice seguente viene utilizzata la Headers raccolta 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 ServicePoint, HttpWebRequest, e WebClient sono obsoleti e non è consigliabile usarli per il nuovo sviluppo. Utilizzare invece HttpClient.

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

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

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 WebClient oggetto . 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 HttpWebRequest classe ha proprietà per l'impostazione di alcune delle intestazioni precedenti. Se è importante che un'applicazione imposti queste intestazioni, la HttpWebRequest classe deve essere usata anziché la WebRequest classe .

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