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

要求に関連付けられているヘッダーの名前/値ペアを格納する WebHeaderCollectionA 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.

一部の共通ヘッダーは制限付きと見なされ、システムによって保護され、オブジェクトで設定または変更することはできません WebHeaderCollectionSome common headers are considered restricted and are protected by the system and cannot be set or changed in a WebHeaderCollection object. オブジェクトに関連付けられているオブジェクトでこれらの制限付きヘッダーのいずれかを設定しようとすると、 WebHeaderCollection WebClient 後で要求を送信しようとすると例外がスローされ 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

  • 接続Connection

  • Content-LengthContent-Length

  • (値が "100-continue" に設定されている場合)Expect (when the value is set to "100-continue"

  • If-Modified-SinceIf-Modified-Since

  • RangeRange

  • 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.

適用対象

こちらもご覧ください