HttpWebRequest.Headers Propiedad

Definición

Especifica una colección de los pares nombre/valor que componen los encabezados 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

Valor de propiedad

WebHeaderCollection

WebHeaderCollection que contiene los pares nombre-valor que componen los encabezados de la solicitud HTTP.A WebHeaderCollection that contains the name/value pairs that make up the headers for the HTTP request.

Excepciones

Ejemplos

En el ejemplo de código siguiente se usa la Headers propiedad para imprimir los pares de nombre/valor del encabezado HTTP en la consola.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()

Comentarios

La Headers colección contiene los encabezados de protocolo asociados a la solicitud.The Headers collection contains the protocol headers associated with the request. En la tabla siguiente se enumeran los encabezados HTTP que no se almacenan en la Headers colección, pero que se establecen mediante el sistema o se establecen mediante propiedades o métodos.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.

EncabezadoHeader Establecido porSet by
AceptarAccept Establecido por la Accept propiedad.Set by the Accept property.
ConexiónConnection Establecido por la Connection propiedad y la KeepAlive propiedad.Set by the Connection property and KeepAlive property.
Content-LengthContent-Length Establecido por la ContentLength propiedad.Set by the ContentLength property.
Content-TypeContent-Type Establecido por la ContentType propiedad.Set by the ContentType property.
ExpectExpect Establecido por la Expect propiedad.Set by the Expect property.
DateDate Establecido por la Date propiedad.Set by the Date property.
administrador de flujos de trabajoHost Establecido por la Host propiedad.Set by the Host property.
If-Modified-SinceIf-Modified-Since Establecido por la IfModifiedSince propiedad.Set by the IfModifiedSince property.
IntervaloRange Establecido por el AddRange método.Set by the AddRange method.
RefererReferer Establecido por la Referer propiedad.Set by the Referer property.
Transfer-EncodingTransfer-Encoding Establecido por la TransferEncoding propiedad (la SendChunked propiedad debe ser true).Set by the TransferEncoding property (the SendChunked property must be true).
User-AgentUser-Agent Establecido por la UserAgent propiedad.Set by the UserAgent property.

El Add método produce una excepción ArgumentException si se intenta establecer uno de estos encabezados protegidos.The Add method throws an ArgumentException if you try to set one of these protected headers.

El cambio de la Headers propiedad después de que la solicitud se ha iniciado mediante una llamada al GetRequestStream BeginGetRequestStream método,, GetResponse o BeginGetResponse produce una excepción InvalidOperationException .Changing the Headers property after the request has been started by calling GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method throws an InvalidOperationException.

No debe preocuparse de que los valores de encabezado no se modificarán, ya que los servidores web y las cachés pueden cambiar o agregar encabezados a una solicitud 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.

Se aplica a