WebClient.Headers Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
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 alla richiesta.
Esempio
Nell'esempio di codice seguente viene utilizzata la Headers raccolta per impostare l'intestazione HTTP Content-Type
per application/x-www-form-urlencoded,
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
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:
Data
Host
Inoltre, alcune altre intestazioni sono limitate quando si usa un WebClient oggetto . Queste intestazioni con restrizioni includono, ma non sono limitate alle seguenti:
Accetta
Connessioni
Content-Length
Previsto (quando il valore è impostato su "100-continue"
If-Modified-Since
Range
Transfer-encoding
La HttpWebRequest classe ha proprietà per l'impostazione di alcune delle intestazioni precedenti. Se è importante che un'applicazione imposti queste intestazioni, è necessario usare la HttpWebRequest classe anziché la WebRequest classe .
Non è consigliabile presupporre che i valori di intestazione rimangano invariati, perché i server Web e le cache possono modificare o aggiungere intestazioni a una richiesta Web.
Si applica a
Vedi anche
Commenti e suggerimenti
https://aka.ms/ContentUserFeedback.
Presto disponibile: Nel corso del 2024 verranno gradualmente disattivati i problemi di GitHub come meccanismo di feedback per il contenuto e ciò verrà sostituito con un nuovo sistema di feedback. Per altre informazioni, vedereInvia e visualizza il feedback per