WebClient.DownloadData 方法

定義

從指定的 URI 下載資源做為 Byte 陣列。Downloads the resource as a Byte array from the URI specified.

多載

DownloadData(String)

從指定的 URI 下載資源做為 Byte 陣列。Downloads the resource as a Byte array from the URI specified.

DownloadData(Uri)

從指定的 URI 下載資源做為 Byte 陣列。Downloads the resource as a Byte array from the URI specified.

DownloadData(String)

從指定的 URI 下載資源做為 Byte 陣列。Downloads the resource as a Byte array from the URI specified.

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。The URI from which to download data.

傳回

Byte[]

Byte 陣列,包含下載的資源。A Byte array containing the downloaded resource.

例外狀況

address 參數為 nullThe address parameter is null.

合併 BaseAddressaddress 而形成的 URI 是無效的。The URI formed by combining BaseAddress and address is invalid.

-或--or-

下載資料時發生錯誤。An error occurred while downloading data.

已經在多個執行緒上同時呼叫此方法。The method has been called simultaneously on multiple threads.

範例

下列程式碼範例會從伺服器要求資料,並顯示傳回的資料。The following code example requests data from a server and displays the data returned. 它會假設 remoteUri 包含所要求資料的有效 URI。It assumes that remoteUri contains a valid URI for the requested data.

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方法會下載具有參數所指定之 URI 的資源 addressThe DownloadData method downloads the resource with the URI specified by the address parameter. 此方法會在下載資源時封鎖。This method blocks while downloading the resource. 若要在等候伺服器的回應時下載資源並繼續執行,請使用其中一種 DownloadDataAsync 方法。To download a resource and continue executing while waiting for the server's response, use one of the DownloadDataAsync methods.

如果 BaseAddress 屬性不是空字串 ( "" ) 且不 address 包含絕對 uri, address 則必須是與結合 BaseAddress 以形成所要求資料絕對 uri 的相對 uri。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果 QueryString 屬性不是空字串,則會附加至 addressIf the QueryString property is not an empty string, it is appended to address.

這個方法會使用 RETR 命令來下載 FTP 資源。This method uses the RETR command to download an FTP resource. 若為 HTTP 資源,則會使用 GET 方法。For an HTTP resource, the GET method is used.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤For more information, see Network Tracing in .NET Framework.

適用於

DownloadData(Uri)

從指定的 URI 下載資源做為 Byte 陣列。Downloads the resource as a Byte array from the URI specified.

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,可以從其中下載資料。The URI represented by the Uri object, from which to download data.

傳回

Byte[]

Byte 陣列,包含下載的資源。A Byte array containing the downloaded resource.

例外狀況

address 參數為 nullThe address parameter is null.

備註

DownloadData方法會下載具有參數所指定之 URI 的資源 addressThe DownloadData method downloads the resource with the URI specified by the address parameter. 此方法會在下載資源時封鎖。This method blocks while downloading the resource. 若要在等候伺服器的回應時下載資源並繼續執行,請使用其中一種 DownloadDataAsync 方法。To download a resource and continue executing while waiting for the server's response, use one of the DownloadDataAsync methods.

如果 BaseAddress 屬性不是空字串 ( "" ) 且不 address 包含絕對 uri, address 則必須是與結合 BaseAddress 以形成所要求資料絕對 uri 的相對 uri。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果 QueryString 屬性不是空字串,則會附加至 addressIf the QueryString property is not an empty string, it is appended to address.

這個方法會使用 RETR 命令來下載 FTP 資源。This method uses the RETR command to download an FTP resource. 若為 HTTP 資源,則會使用 GET 方法。For an HTTP resource, the GET method is used.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤For more information, see Network Tracing in .NET Framework.

適用於