WebClient.Headers 屬性

定義

取得或設定與要求相關聯標頭名稱/值組合的集合。

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

WebHeaderCollection,包含與這個要求相關聯的標頭名稱/值組合。

範例

下列程式碼範例會 Headers 使用 集合來設定 HTTP Content-Type 標頭, application/x-www-form-urlencoded, 以通知伺服器表單資料已附加至張貼。

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 要求一起傳送的通訊協定標頭。

某些常見的標頭會被視為受限制,而且受到系統保護,而且無法在 物件中 WebHeaderCollection 設定或變更。 嘗試在與 WebClient 物件相關聯的 物件中 WebHeaderCollection 設定其中一個受限制的標頭,稍後嘗試傳送 WebClient 要求時,都會擲回例外狀況。

受系統保護的受限制標頭包括,但不限於下列各項:

  • Date

  • Host

此外,使用 WebClient 物件時也會限制一些其他標頭。 這些受限制的標頭包括,但不限於下列各項:

  • 接受

  • 連線

  • Content-Length

  • 當值設定為 「100-continue」 時,預期 (

  • If-Modified-Since

  • 範圍

  • Transfer-Encoding

類別 HttpWebRequest 具有設定上述部分標頭的屬性。 如果應用程式必須設定這些標頭, HttpWebRequest 則應該使用 類別,而不是 WebRequest 類別。

您不應該假設標頭值會保持不變,因為 Web 服務器和快取可能會變更或新增標頭至 Web 要求。

適用於

另請參閱