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 する場合は、他のヘッダーも制限されます。 これらの制限付きヘッダーには、次のものが含まれますが、これらに限定されません。

  • Accept

  • 接続

  • Content-Length

  • Expect (値が "100-continue" に設定されている場合)

  • If-Modified-Since

  • Range

  • Transfer-Encoding

この HttpWebRequest クラスには、上記のヘッダーの一部を設定するためのプロパティがあります。 アプリケーションでこれらのヘッダーを設定することが重要な場合は、 HttpWebRequest クラスの代わりにクラスを使用する WebRequest 必要があります。

Web サーバーとキャッシュは Web 要求にヘッダーを変更または追加する可能性があるため、ヘッダー値は変更されません。

適用対象

こちらもご覧ください