次の方法で共有


WebClient.DownloadData メソッド

指定した URI を持つリソースからデータをダウンロードします。

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

パラメータ

  • address
    データのダウンロード元の URI。

戻り値

address パラメータで指定したリソースからダウンロードしたデータを格納するバイト配列。

例外

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

または

データのダウンロード中にエラーが発生しました。

解説

DownloadData メソッドは、 address パラメータで指定した URI からローカル バイト配列にデータをダウンロードします。

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

使用例

[Visual Basic, C#, C++] サーバーからデータを要求し、返されるデータを表示する例を次に示します。 remoteUri に要求したデータの有効な URI が格納されていることを前提にしています。

 
Try
    Console.Write(ControlChars.Cr + "Please enter a Url(e.g. https://www.msn.com): ")
    Dim remoteUrl As String = Console.ReadLine()            
    ' Create a new WebClient instance.
    Dim myWebClient As New WebClient()
    ' Download the home page data.
    Console.WriteLine(("Downloading " + remoteUrl))
    ' DownloadData() method takes a 'uriRemote.ToString()' and downloads the Web resource and saves it into a data buffer.
    Dim myDatabuffer As Byte() = myWebClient.DownloadData(remoteUrl)

    ' Display the downloaded data.
Dim download as String = Encoding.ASCII.GetString(myDataBuffer)
    Console.WriteLine(download)

    Console.WriteLine("Download successful.")

[C#] 
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Download home page data.
Console.WriteLine("Downloading " + remoteUri);                        
// Download the Web resource and save it into a data buffer.
byte[] myDataBuffer = myWebClient.DownloadData (remoteUri);

// Display the downloaded data.
string download = Encoding.ASCII.GetString(myDataBuffer);
Console.WriteLine(download);
                    
Console.WriteLine("Download successful.");

[C++] 
// Create a new WebClient instance.
WebClient* myWebClient = new WebClient();
// Download home page data.
Console::WriteLine(S"Downloading {0}", remoteUri);
// Download the Web resource and save it into a data buffer.
Byte myDataBuffer[] = myWebClient->DownloadData (remoteUri);

// Display the downloaded data.
String* download = Encoding::ASCII->GetString(myDataBuffer);
Console::WriteLine(download);

Console::WriteLine(S"Download successful.");

[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 名前空間