HttpWebRequest.Headers Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Určuje kolekci dvojic název/hodnota, které tvoří hlavičky PROTOKOLU 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
Hodnota vlastnosti
A WebHeaderCollection , který obsahuje páry název/hodnota, které tvoří hlavičky pro požadavek HTTP.
Výjimky
Požadavek byl zahájen voláním GetRequestStream()metody , BeginGetRequestStream(AsyncCallback, Object), GetResponse(), nebo BeginGetResponse(AsyncCallback, Object) .
Příklady
Následující příklad kódu používá Headers vlastnost k tisku dvojice název/hodnota hlavičky HTTP do konzoly.
// 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()
Poznámky
Kolekce Headers obsahuje hlavičky protokolu přidružené k požadavku. Následující tabulka uvádí hlavičky PROTOKOLU HTTP, které nejsou uloženy v kolekci Headers , ale jsou buď nastaveny systémem, nebo vlastnostmi nebo metodami.
Hlavička | Nastavit podle |
---|---|
Přijmout | Nastaví se Accept vlastností . |
Připojení | Nastavte vlastnostmi Connection a KeepAlive vlastnostmi. |
Délka obsahu | Nastaví se ContentLength vlastností . |
Typ obsahu | Nastaví se ContentType vlastností . |
Očekávat | Nastaví se Expect vlastností . |
Datum | Nastaví se Date vlastností . |
Host | Nastaví se Host vlastností . |
If-Modified-Since | Nastaví se IfModifiedSince vlastností . |
Rozsah | Nastaveno metodou AddRange . |
Referer | Nastaví se Referer vlastností . |
Transfer-Encoding | Nastaveno TransferEncoding vlastností ( SendChunked vlastnost musí být true). |
User-Agent | Nastaví se UserAgent vlastností . |
Metoda Add vyvolá výjimku , ArgumentException pokud se pokusíte nastavit jednu z těchto chráněných hlaviček.
Headers Změna vlastnosti po spuštění požadavku voláním GetRequestStreammetody , BeginGetRequestStream, GetResponse, nebo BeginGetResponse vyvolá výjimku InvalidOperationException.
Neměli byste předpokládat, že hodnoty hlaviček zůstanou nezměněné, protože webové servery a mezipaměti se mohou změnit nebo přidat hlavičky do webového požadavku.
Platí pro
Váš názor
https://aka.ms/ContentUserFeedback.
Připravujeme: V průběhu roku 2024 budeme postupně vyřazovat problémy z GitHub coby mechanismus zpětné vazby pro obsah a nahrazovat ho novým systémem zpětné vazby. Další informace naleznete v tématu:Odeslat a zobrazit názory pro