次の方法で共有


WebClient.UploadValues メソッド (String, NameValueCollection)

指定した名前/値コレクションを URI で識別される指定したリソースへアップロードします。

Overloads Public Function UploadValues( _
   ByVal address As String, _   ByVal data As NameValueCollection _) As Byte()
[C#]
public byte[] UploadValues(stringaddress,NameValueCollectiondata);
[C++]
public: unsigned char UploadValues(String* address,NameValueCollection* data)  __gc[];
[JScript]
public function UploadValues(
   address : String,data : NameValueCollection) : Byte[];

パラメータ

  • address
    コレクションを受信するリソースの URI。
  • data
    リソースに送信する NameValueCollection

戻り値

リソースからの応答の本体を保持しているバイトの配列。

例外

例外の種類 条件
WebException BaseAddress および address を組み合わせて形成された URI が無効です。

または

data が null 参照 (Visual Basic では Nothing) です。

または

リソースをホストしているサーバーから応答がありませんでした。

または

ストリームのオープン中にエラーが発生しました。

または

Content-type ヘッダーが null 参照 (Nothing) または "application/x-www-form-urlencoded" ではありません。

解説

UploadValues メソッドは、インターネット サーバーに NameValueCollection を送信します。基になる要求は、メソッド動詞 POST を使用して作成されています。

動詞 POST は、HTTP によって定義されます。基になる要求が HTTP を使用せず、POST がサーバーによって認識されない場合は、発生する内容は、基になるプロトコル クラスによって決定されます。通常は、エラーを示す Status プロパティ セットと共に WebException がスローされます。

Content-type ヘッダーが null 参照 (Visual Basic では Nothing) の場合、 UploadValues メソッドは、Content-type ヘッダーを "Content-type: application/x-www-form-urlencoded" に設定します。

BaseAddress プロパティが空でない場合、 address は、相対 URI である必要があります。この相対 URI は、 BaseAddress と結合されて、要求されたデータの絶対 URI を形成します。 QueryString プロパティが空でない場合は、 address に追加されます。

使用例

[Visual Basic, C#, C++] ユーザーから情報 (名前、年齢、および住所) を収集し、 UploadValues を使用して、それらの値をサーバーにポスト バックする例を次に示します。サーバーからの応答は、コンソールに表示されます。

 
Console.Write(ControlChars.Cr + "Please enter the URI to post data to : ")
Dim uriString As String = Console.ReadLine()
' Create a new WebClient instance.
Dim myWebClient As New WebClient()
' Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
Dim myNameValueCollection As New NameValueCollection()
Console.WriteLine("Please enter the following parameters to be posted to the URL:")
Console.Write("Name:")
Dim name As String = Console.ReadLine()

Console.Write("Age:")
Dim age As String = Console.ReadLine()

Console.Write("Address:")
Dim address As String = Console.ReadLine()

' Add necessary parameter/value pairs to the name/value container.
myNameValueCollection.Add("Name", name)
myNameValueCollection.Add("Address", address)
myNameValueCollection.Add("Age", age)

Console.WriteLine(ControlChars.Cr + "Uploading to {0} ...", uriString)
' The Upload(String,NameValueCollection)' method implicitly sets the HTTP POST as the request method.            
Dim responseArray As Byte() = myWebClient.UploadValues(uriString, myNameValueCollection)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :" + ControlChars.Cr + "{0}", Encoding.ASCII.GetString(responseArray))

[C#] 
Console.Write("\nPlease enter the URI to post data to : ");
string uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

// Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
NameValueCollection myNameValueCollection = new NameValueCollection();

Console.WriteLine("Please enter the following parameters to be posted to the URL");
Console.Write("Name:");
string name = Console.ReadLine();

Console.Write("Age:");
string age = Console.ReadLine();

Console.Write("Address:");
string address = Console.ReadLine();

// Add necessary parameter/value pairs to the name/value container.
myNameValueCollection.Add("Name",name);            
myNameValueCollection.Add("Address",address);
myNameValueCollection.Add("Age",age);

Console.WriteLine("\nUploading to {0} ...",  uriString);
// 'The Upload(String,NameValueCollection)' implicitly method sets HTTP POST as the request method.            
byte[] responseArray = myWebClient.UploadValues(uriString,myNameValueCollection);

// Decode and display the response.
Console.WriteLine("\nResponse received was :\n{0}",Encoding.ASCII.GetString(responseArray));

[C++] 
Console::Write(S"\nPlease enter the URI to post data to : ");
String* uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient* myWebClient = new WebClient();

// Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
NameValueCollection* myNameValueCollection =
   new NameValueCollection();

Console::WriteLine(S"Please enter the following parameters to be posted to the URL");
Console::Write(S"Name:");
String* name = Console::ReadLine();

Console::Write(S"Age:");
String* age = Console::ReadLine();

Console::Write(S"Address:");
String* address = Console::ReadLine();

// Add necessary parameter/value pairs to the name/value container.
myNameValueCollection->Add(S"Name", name);
myNameValueCollection->Add(S"Address", address);
myNameValueCollection->Add(S"Age", age);

Console::WriteLine(S"\nUploading to {0} ...",  uriString);
// 'The Upload(String, NameValueCollection)' implicitly method sets HTTP POST as the request method.
Byte responseArray[] = myWebClient->UploadValues(uriString, myNameValueCollection);

// Decode and display the response.
Console::WriteLine(S"\nResponse received was :\n {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 名前空間 | WebClient.UploadValues オーバーロードの一覧