HttpWebRequest.Headers 屬性


指定組成 HTTP 標頭的名稱/值組集合。Specifies a collection of the name/value pairs that make up the HTTP headers.

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



WebHeaderCollection,包含組成 HTTP 要求之標頭的名稱/值組。A WebHeaderCollection that contains the name/value pairs that make up the headers for the HTTP request.



下列程式碼範例會使用 Headers 屬性,將 HTTP 標頭名稱/值組列印到主控台。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( "" ) );
// 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 );
// Release the HttpWebResponse Resource.
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("");
// 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);
    count = streamRead.Read(readBuff, 0, 256);
// Close the Stream object.
// Release the HttpWebResponse Resource.
     ' Create a new 'HttpWebRequest' Object to the mentioned URL.
     Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(""), 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)
         count = streamRead.Read(readBuff, 0, 256)
     End While
' Close the Stream object.
' Release the HttpWebResponse Resource.


Headers集合包含與要求相關聯的通訊協定標頭。The Headers collection contains the protocol headers associated with the request. 下表列出未儲存在集合中, Headers 但由系統設定,或由屬性或方法設定的 HTTP 標頭。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.

標頭Header 設定依據Set by
接受Accept 由屬性設定 AcceptSet by the Accept property.
ConnectionConnection Connection 屬性和屬性設定 KeepAliveSet by the Connection property and KeepAlive property.
Content-LengthContent-Length 由屬性設定 ContentLengthSet by the ContentLength property.
Content-TypeContent-Type 由屬性設定 ContentTypeSet by the ContentType property.
ExpectExpect 由屬性設定 ExpectSet by the Expect property.
DateDate 由屬性設定 DateSet by the Date property.
HostHost 由屬性設定 HostSet by the Host property.
If-Modified-SinceIf-Modified-Since 由屬性設定 IfModifiedSinceSet by the IfModifiedSince property.
範圍Range 由方法設定 AddRangeSet by the AddRange method.
RefererReferer 由屬性設定 RefererSet by the Referer property.
Transfer-EncodingTransfer-Encoding 由屬性設定 TransferEncoding (SendChunked 屬性必須為 true) 。Set by the TransferEncoding property (the SendChunked property must be true).
User-AgentUser-Agent 由屬性設定 UserAgentSet by the UserAgent property.

Add ArgumentException 如果您嘗試設定其中一個受保護的標頭,則方法會擲回。The Add method throws an ArgumentException if you try to set one of these protected headers.

Headers 由呼叫 GetRequestStream 、、 BeginGetRequestStream GetResponseBeginGetResponse 方法 InvalidOperationException 來啟動要求之後變更屬性,會擲回。Changing the Headers property after the request has been started by calling GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method throws an InvalidOperationException.

您不應該假設標頭值會維持不變,因為 Web 服務器和快取可能會變更,或將標頭新增至 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.