Condividi tramite


HttpWebRequest.Headers Proprietà

Definizione

Specifica una raccolta delle coppie nome/valore che costituiscono le intestazioni HTTP.

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

Valore della proprietà

Oggetto WebHeaderCollection contenente le coppie nome/valore che costituiscono le intestazioni per la richiesta HTTP.

Eccezioni

Esempio

Nell'esempio di codice seguente viene utilizzata la Headers proprietà per stampare le coppie nome/valore dell'intestazione HTTP nella console.

// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}",myHttpWebRequest.Headers);
// Print the HTML contents of the page to the console.
Stream streamResponse=myHttpWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader( streamResponse );
Char[] readBuff = new Char[256];
int count = streamRead.Read( readBuff, 0, 256 );
Console.WriteLine("\nThe HTML contents of page the are  : \n\n ");	
while (count > 0)
{
    String outputData = new String(readBuff, 0, count);
    Console.Write(outputData);
    count = streamRead.Read(readBuff, 0, 256);
}
// Close the Stream object.
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse Resource.
myHttpWebResponse.Close();
     ' Create a new 'HttpWebRequest' Object to the mentioned URL.
     Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.contoso.com"), HttpWebRequest)
     ' Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
     Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
     Console.WriteLine(ControlChars.Cr + "The HttpHeaders are " + ControlChars.Cr + ControlChars.Cr + ControlChars.Tab + "Name" + ControlChars.Tab + ControlChars.Tab + "Value" + ControlChars.Cr + "{0}", myHttpWebRequest.Headers)

     ' Print the HTML contents of the page to the console. 
     Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
     Dim streamRead As New StreamReader(streamResponse)
     Dim readBuff(256) As [Char]
     Dim count As Integer = streamRead.Read(readBuff, 0, 256)
     Console.WriteLine(ControlChars.Cr + "The HTML contents of page the are  : " + ControlChars.Cr + ControlChars.Cr + " ")
     While count > 0
         Dim outputData As New [String](readBuff, 0, count)
         Console.Write(outputData)
         count = streamRead.Read(readBuff, 0, 256)
     End While
' Close the Stream object.
streamResponse.Close()
streamRead.Close()
' Release the HttpWebResponse Resource.
 myHttpWebResponse.Close()

Commenti

Cautela

WebRequest ServicePoint, HttpWebRequest, e WebClient sono obsoleti e non è consigliabile usarli per il nuovo sviluppo. Utilizzare invece HttpClient.

La Headers raccolta contiene le intestazioni di protocollo associate alla richiesta. Nella tabella seguente sono elencate le intestazioni HTTP non archiviate nella Headers raccolta, ma impostate dal sistema o impostate da proprietà o metodi.

Intestazione Impostato da
Accettare Impostata dalla Accept proprietà .
Connessione Impostata dalla proprietà e KeepAlive dalla Connection proprietà .
Lunghezza contenuto Impostata dalla ContentLength proprietà .
Tipo di contenuto Impostata dalla ContentType proprietà .
Aspettare Impostata dalla Expect proprietà .
Dattero Impostata dalla Date proprietà .
Ospite Impostata dalla Host proprietà .
If-Modified-Since Impostata dalla IfModifiedSince proprietà .
Gamma Impostato dal AddRange metodo .
Referer Impostata dalla Referer proprietà .
Transfer-Encoding Impostata dalla TransferEncoding proprietà (la SendChunked proprietà deve essere true).
User-Agent Impostata dalla UserAgent proprietà .

Il Add metodo genera un'eccezione ArgumentException se si tenta di impostare una di queste intestazioni protette.

La modifica della proprietà dopo l'avvio Headers della richiesta chiamando GetRequestStreamil metodo , BeginGetRequestStreamGetResponse, o BeginGetResponse genera un'eccezione InvalidOperationException.

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