WebClient.DownloadData WebClient.DownloadData WebClient.DownloadData WebClient.DownloadData Method

定義

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

多載

DownloadData(String) DownloadData(String) DownloadData(String) DownloadData(String)

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

DownloadData(Uri) DownloadData(Uri) DownloadData(Uri) DownloadData(Uri)

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

DownloadData(String) DownloadData(String) DownloadData(String) 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 String String 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 下載資源address參數。The 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必須是相對 URI,並結合了BaseAddress形成要求資料的絕對 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.

此方法會使用 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 the .NET Framework.

DownloadData(Uri) DownloadData(Uri) DownloadData(Uri) 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 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 下載資源address參數。The 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必須是相對 URI,並結合了BaseAddress形成要求資料的絕對 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.

此方法會使用 FTP 資源下載試命令。This method uses the RETR command to download an FTP resource. 對於 HTTP 資源,可使用 GET 方法。For an HTTP resource, the GET method is used.

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

適用於