WebClient.Headers 屬性

定義

取得或設定與要求相關聯標頭名稱/值組合的集合。Gets or sets a collection of header name/value pairs associated with the request.

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

屬性值

WebHeaderCollection,包含與這個要求相關聯的標頭名稱/值組合。A WebHeaderCollection containing header name/value pairs associated with this request.

範例

下列程式碼範例會使用 Headers 集合,將 HTTP Content-Type 標頭設定為 application/x-www-form-urlencoded,,以通知伺服器表單資料已附加至 post。The following code example uses the Headers collection to set the HTTP Content-Type header to application/x-www-form-urlencoded, to notify the server that form data is attached to the post.

String^ uriString;
Console::Write( "\nPlease enter the URI to post data to {for example, http://www.contoso.com}: " );
uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine( "\nPlease enter the data to be posted to the URI {0}:", uriString );
String^ postData = Console::ReadLine();
myWebClient->Headers->Add( "Content-Type", "application/x-www-form-urlencoded" );

// Displays the headers in the request
Console::Write( "Resulting Request Headers: ");
Console::WriteLine(myWebClient->Headers);

// Apply ASCII Encoding to obtain the String^ as a Byte array.
array<Byte>^ byteArray = Encoding::ASCII->GetBytes( postData );
Console::WriteLine( "Uploading to {0} ...", uriString );
// Upload the input String* using the HTTP 1.0 POST method.
array<Byte>^responseArray = myWebClient->UploadData( uriString, "POST", byteArray );
// Decode and display the response.
Console::WriteLine( "\nResponse received was {0}",
   Encoding::ASCII->GetString( responseArray ) );
   string uriString;
       Console.Write("\nPlease enter the URI to post data to {for example, http://www.contoso.com} : ");
       uriString = Console.ReadLine();

       // Create a new WebClient instance.
       WebClient myWebClient = new WebClient();
       Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
       string postData = Console.ReadLine();
       myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

 // Display the headers in the request
       Console.Write("Resulting Request Headers: ");
       Console.WriteLine(myWebClient.Headers.ToString());
       
       // Apply ASCII Encoding to obtain the string as a byte array.

       byte[] byteArray = Encoding.ASCII.GetBytes(postData);
       Console.WriteLine("Uploading to {0} ...",  uriString);						
       // Upload the input string using the HTTP 1.0 POST method.
       byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
       
       // Decode and display the response.
       Console.WriteLine("\nResponse received was {0}",
       Encoding.ASCII.GetString(responseArray));
                 
Dim uriString As String
Console.Write(ControlChars.Cr + "Please enter the URI to post data to{for example, http://www.contoso.com} : ")
uriString = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()
Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the URI {0}:", uriString)
Dim postData As String = Console.ReadLine()
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

' Display the headers in the request
Console.Write("Resulting Request Headers: ")
Console.Writeline(myWebClient.Headers.ToString())

' Apply ASCII Encoding to obtain the string as a byte array.
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uriString)
' Upload the input string using the HTTP 1.0 POST method.
Dim responseArray As Byte() = myWebClient.UploadData(uriString, "POST", byteArray)
' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :{0}", Encoding.ASCII.GetString(responseArray))

備註

Headers 屬性包含 WebHeaderCollection 實例,其中包含 WebClient 與要求一起傳送的通訊協定標頭。The Headers property contains a WebHeaderCollection instance containing protocol headers that the WebClient sends with the request.

某些常見的標頭會被視為受限制,並受到系統的保護,而且無法在 WebHeaderCollection 物件中設定或變更。Some common headers are considered restricted and are protected by the system and cannot be set or changed in a WebHeaderCollection object. 嘗試在與 WebClient 物件相關聯的 WebHeaderCollection 物件中設定其中一個限制標頭,將會在稍後嘗試傳送 WebClient 要求時擲回例外狀況。Any attempt to set one of these restricted headers in the WebHeaderCollection object associated with a WebClient object will throw an exception later when attempting to send the WebClient request.

受系統保護的受限標頭包括但不限於下列各項:Restricted headers protected by the system include, but are not limited to the following:

  • DateDate

  • HostHost

此外,使用 WebClient 物件時,也會限制某些其他標頭。In addition, some other headers are also restricted when using a WebClient object. 這些限制的標頭包括但不限於下列各項:These restricted headers include, but are not limited to the following:

  • AcceptAccept

  • ConnectionConnection

  • Content-LengthContent-Length

  • 預期(當值設定為 "100-continue" 時)Expect (when the value is set to "100-continue"

  • If-Modified-SinceIf-Modified-Since

  • 範圍Range

  • Transfer-EncodingTransfer-Encoding

HttpWebRequest 類別具有設定上述標頭的屬性。The HttpWebRequest class has properties for setting some of the above headers. 如果應用程式必須設定這些標頭,則應該使用 HttpWebRequest 類別,而不是 WebRequest 類別。If it is important for an application to set these headers, then the HttpWebRequest class should be used instead of the WebRequest class.

您不應該假設標頭值會維持不變,因為 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.

適用於

另請參閱