WebClient.Headers プロパティ
要求に関連付けられているヘッダーの名前/値ペアのコレクションを取得または設定します。
Public Property Headers As WebHeaderCollection
[C#]
public WebHeaderCollection Headers {get; set;}
[C++]
public: __property WebHeaderCollection* get_Headers();public: __property void set_Headers(WebHeaderCollection*);
[JScript]
public function get Headers() : WebHeaderCollection;public function set Headers(WebHeaderCollection);
プロパティ値
要求に関連付けられているヘッダーの名前/値ペアを格納する WebHeaderCollection 。
解説
Headers プロパティは、 WebClient がインターネット リソースに送信するヘッダー情報を含む WebHeaderCollection インスタンスを格納します。これは無制限のヘッダーのコレクションであるため、 HttpWebRequest などの WebRequest の子孫で制限されるヘッダーを設定できます。
使用例
[Visual Basic, C#, C++] Headers コレクションを使用して、HTTP Content-Type ヘッダーを "application/x-www-form-urlencoded" に設定して、フォーム データがポスト バックに接続されることをサーバーに通知する例を次に示します。
Dim uriString As String
Console.Write(ControlChars.Cr + "Please enter the URI to post data to{for example, https://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")
' 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))
[C#]
string uriString;
Console.Write("\nPlease enter the URI to post data to {for example, https://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");
// 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));
[C++]
String* uriString;
Console::Write(S"\nPlease enter the URI to post data to {for example, https://www.contoso.com} : ");
uriString = Console::ReadLine();
// Create a new WebClient instance.
WebClient* myWebClient = new WebClient();
Console::WriteLine(S"\nPlease enter the data to be posted to the URI {0}:", uriString);
String* postData = Console::ReadLine();
myWebClient->Headers->Add(S"Content-Type", S"application/x-www-form-urlencoded");
// Apply ASCII Encoding to obtain the String* as a Byte array.
Byte byteArray[] = Encoding::ASCII->GetBytes(postData);
Console::WriteLine(S"Uploading to {0} ...", uriString);
// Upload the input String* using the HTTP 1.0 POST method.
Byte responseArray[] = myWebClient->UploadData(uriString, S"POST", byteArray);
// Decode and display the response.
Console::WriteLine(S"\nResponse received was {0}",
Encoding::ASCII->GetString(responseArray));
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard
参照
WebClient クラス | WebClient メンバ | System.Net 名前空間 | WebRequest.Headers | HttpWebRequest.Headers