WebClient.DownloadData 方法

定義

從指定的 URI 下載資源做為 Byte 陣列。

多載

DownloadData(String)

從指定的 URI 下載資源做為 Byte 陣列。

DownloadData(Uri)

從指定的 URI 下載資源做為 Byte 陣列。

DownloadData(String)

從指定的 URI 下載資源做為 Byte 陣列。

public:
 cli::array <System::Byte> ^ DownloadData(System::String ^ address);
public byte[] DownloadData (string address);
member this.DownloadData : string -> byte[]
Public Function DownloadData (address As String) As Byte()

參數

address
String

要從中下載資料的 URI。

傳回

Byte[]

Byte 陣列,包含下載的資源。

例外狀況

address 參數為 null

合併 BaseAddressaddress 而形成的 URI 是無效的。

-或-

下載資料時發生錯誤。

已經在多個執行緒上同時呼叫此方法。

範例

下列程式碼範例會向伺服器要求資料,並顯示傳回的資料。 它假設 remoteUri 包含所要求資料的有效 URI。

Console::Write( "\nPlease enter a URI (e.g. http://www.contoso.com): " );
String^ remoteUri = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
// Download home page data.
Console::WriteLine( "Downloading {0}", remoteUri );
// Download the Web resource and save it into a data buffer.
array<Byte>^ myDataBuffer = myWebClient->DownloadData( remoteUri );

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

Console::WriteLine( "Download successful." );
Console.Write("\nPlease enter a URI (for example, http://www.contoso.com): ");
string remoteUri = Console.ReadLine();

// 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.");

Console.Write(ControlChars.Cr + "Please enter a Url(for example, http://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.")

備註

方法 DownloadData 會使用 參數指定的 address URI 下載資源。 這個方法會在下載資源時封鎖。 若要下載資源,並在等候伺服器的回應時繼續執行,請使用其中 DownloadDataAsync 一種方法。

BaseAddress如果屬性不是空字串 (「」) 且 address 不包含絕對 URI, address 則必須是結合 BaseAddress 的相對 URI,才能形成所要求資料的絕對 URI。 QueryString如果屬性不是空字串,則會附加至 address

此方法會使用 RETR 命令來下載 FTP 資源。 針對 HTTP 資源,會使用 GET 方法。

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework 中的網路追蹤

適用於

DownloadData(Uri)

從指定的 URI 下載資源做為 Byte 陣列。

public:
 cli::array <System::Byte> ^ DownloadData(Uri ^ address);
public byte[] DownloadData (Uri address);
member this.DownloadData : Uri -> byte[]
Public Function DownloadData (address As Uri) As Byte()

參數

address
Uri

Uri 物件表示的 URI,可以從其中下載資料。

傳回

Byte[]

Byte 陣列,包含下載的資源。

例外狀況

address 參數為 null

備註

方法 DownloadData 會使用 參數所 address 指定的 URI 來下載資源。 這個方法會在下載資源時封鎖。 若要下載資源,並在等候伺服器的回應時繼續執行,請使用其中 DownloadDataAsync 一種方法。

BaseAddress如果屬性不是空字串 (「」) 且 address 不包含絕對 URI, address 則必須是結合 BaseAddress 的相對 URI,才能形成所要求資料的絕對 URI。 QueryString如果屬性不是空字串,則會附加至 address

此方法會使用 RETR 命令來下載 FTP 資源。 針對 HTTP 資源,會使用 GET 方法。

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework 中的網路追蹤

適用於