次の方法で共有


WebClient.UploadData メソッド (String, Byte[])

URI で識別されたリソースへデータ バッファをアップロードします。

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

パラメータ

  • address
    データを受信するリソースの URI。
  • data
    リソースに送信するデータ バッファ。

戻り値

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

例外

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

または

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

または

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

解説

UploadData メソッドは、リソースにデータ バッファを送信します。基になる要求は、メソッド動詞 POST を使用して作成されています。

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

UploadData メソッドは、 data の内容をエンコーディングせずに、サーバーに送信します。

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

使用例

[Visual Basic, C#, C++] コンソールから入力された文字列をバイト配列に変換し、 UploadData を使用して、その配列を指定したサーバーにポスト バックする例を次に示します。サーバーからの応答は、コンソールに表示されます。

 
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()
Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the URI {0}:", uriString)
Dim postData As String = Console.ReadLine()
' Apply ASCII Encoding to obtain the string as a byte array.
Dim postArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uriString)
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

' UploadData implicitly sets HTTP POST as the request method.
Dim responseArray As Byte() = myWebClient.UploadData(uriString, postArray)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :{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();
Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
string postData = Console.ReadLine();
// Apply ASCII Encoding to obtain the string as a byte array.
byte[] postArray = Encoding.ASCII.GetBytes(postData);
Console.WriteLine("Uploading to {0} ...",  uriString);                            
         myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");
        
//UploadData implicitly sets HTTP POST as the request method.
byte[] responseArray = myWebClient.UploadData(uriString,postArray);

// Decode and display the response.
Console.WriteLine("\nResponse received was :{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();
Console::WriteLine(S"\nPlease enter the data to be posted to the URI {0}:", uriString);
String* postData = Console::ReadLine();
// Apply ASCII Encoding to obtain the String* as a Byte array.
Byte postArray[] = Encoding::ASCII->GetBytes(postData);
Console::WriteLine(S"Uploading to {0} ...", uriString);
myWebClient->Headers->Add(S"Content-Type", S"application/x-www-form-urlencoded");

//UploadData implicitly sets HTTP POST as the request method.
Byte responseArray[] = myWebClient->UploadData(uriString, postArray);

// 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 名前空間 | WebClient.UploadData オーバーロードの一覧