HttpWebRequest.Headers HttpWebRequest.Headers HttpWebRequest.Headers HttpWebRequest.Headers Property

Définition

Spécifie une collection de paires nom-valeur qui composent les en-têtes HTTP.Specifies a collection of the name/value pairs that make up the HTTP headers.

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

Valeur de propriété

WebHeaderCollection contenant les paires nom-valeur qui composent les en-têtes de la requête HTTP.A WebHeaderCollection that contains the name/value pairs that make up the headers for the HTTP request.

Exceptions

Exemples

L’exemple de code suivant utilise Headers la propriété pour imprimer les paires nom/valeur d’en-tête http dans la console.The following code example uses the Headers property to print the HTTP header name/value pairs to the 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 = gcnew StreamReader( streamResponse );
array<Char>^ readBuff = gcnew array<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 = gcnew String( readBuff,0,count );
   Console::Write( outputData );
   count = streamRead->Read( readBuff, 0, 256 );
}
streamResponse->Close();
streamRead->Close();
// Release the HttpWebResponse Resource.
myHttpWebResponse->Close();
// 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()

Remarques

La Headers collection contient les en-têtes de protocole associés à la demande.The Headers collection contains the protocol headers associated with the request. Le tableau suivant répertorie les en-têtes HTTP qui ne sont pas Headers stockés dans la collection, mais qui sont définis par le système ou définis par des propriétés ou des méthodes.The following table lists the HTTP headers that are not stored in the Headers collection but are either set by the system or set by properties or methods.

HeaderHeader Défini parSet by
ValideAccept Défini par la Accept propriété.Set by the Accept property.
ConnexionConnection Défini par la Connection propriété et KeepAlive la propriété.Set by the Connection property and KeepAlive property.
Content-LengthContent-Length Défini par la ContentLength propriété.Set by the ContentLength property.
Content-TypeContent-Type Défini par la ContentType propriété.Set by the ContentType property.
ElleExpect Défini par la Expect propriété.Set by the Expect property.
DateDate Défini par la Date propriété.Set by the Date property.
HôteHost Défini par la Host propriété.Set by the Host property.
If-Modified-SinceIf-Modified-Since Défini par la IfModifiedSince propriété.Set by the IfModifiedSince property.
PlageRange Défini par la AddRange méthode.Set by the AddRange method.
RefererReferer Défini par la Referer propriété.Set by the Referer property.
Transfert-encodageTransfer-Encoding Défini par la TransferEncoding propriété (la SendChunked propriété doit avoir la valeur true).Set by the TransferEncoding property (the SendChunked property must be true).
Agent utilisateurUser-Agent Défini par la UserAgent propriété.Set by the UserAgent property.

La Add méthode lève une ArgumentException exception si vous essayez de définir l’un de ces en-têtes protégés.The Add method throws an ArgumentException if you try to set one of these protected headers.

La modification Headers de la propriété après le démarrage de la requête GetRequestStreamen BeginGetRequestStreamappelant GetResponsela méthode BeginGetResponse ,, ou lève InvalidOperationExceptionune exception.Changing the Headers property after the request has been started by calling GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method throws an InvalidOperationException.

Vous ne devez pas supposer que les valeurs d’en-tête restent inchangées, car les serveurs Web et les caches peuvent modifier ou ajouter des en-têtes à une requête Web.You should not assume that the header values will remain unchanged, because Web servers and caches may change or add headers to a Web request.

S’applique à