WebClient.DownloadDataAsync 方法

定義

以非同步作業的方式,從指定的 URI 下載資源做為 Byte 陣列。Downloads the resource as a Byte array from the URI specified as an asynchronous operation. 這些方法不會封鎖呼叫執行緒。These methods do not block the calling thread.

多載

DownloadDataAsync(Uri)

以非同步作業的方式,從指定的 URI 下載資源做為 Byte 陣列。Downloads the resource as a Byte array from the URI specified as an asynchronous operation.

DownloadDataAsync(Uri, Object)

以非同步作業的方式,從指定的 URI 下載資源做為 Byte 陣列。Downloads the resource as a Byte array from the URI specified as an asynchronous operation.

DownloadDataAsync(Uri)

以非同步作業的方式,從指定的 URI 下載資源做為 Byte 陣列。Downloads the resource as a Byte array from the URI specified as an asynchronous operation.

public:
 void DownloadDataAsync(Uri ^ address);
public void DownloadDataAsync (Uri address);
member this.DownloadDataAsync : Uri -> unit
Public Sub DownloadDataAsync (address As Uri)

參數

address
Uri

Uri,包含要下載的 URI。A Uri containing the URI to download.

例外狀況

address 參數為 nullThe address parameter is null.

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

-或--or-

下載資源時發生錯誤。An error occurred while downloading the resource.

備註

這個方法會使用與指定之 URI 配置相關聯之通訊協定的預設方法,來抓取指定的資源 addressThis method retrieves the specified resource using the default method for the protocol associated with the URI scheme specified in address. 系統會使用從執行緒集區自動設定的執行緒資源,以非同步方式下載資料。The data is downloaded asynchronously using thread resources that are automatically allocated from the thread pool.

在下載資源時,此方法不會封鎖呼叫執行緒。This method does not block the calling thread while downloading the resource. 若要在等候伺服器的回應時下載資源和封鎖,請使用其中一種 DownloadData 方法。To download a resource and block while waiting for the server's response, use one of the DownloadData methods. 當下載完成時, DownloadDataCompleted 就會引發事件。When the download completes, the DownloadDataCompleted event is raised. 您的應用程式必須處理此事件,才會收到通知。Your application must handle this event to receive notification. 已下載的資料會出現在 Result 屬性中。The downloaded data is available in the Result property.

在 .NET Framework 和 .NET Core 1.0 中,您可以藉由呼叫方法來取消尚未完成的非同步作業 CancelAsyncIn .NET Framework and .NET Core 1.0, you can cancel asynchronous operations that have not completed by calling the CancelAsync method.

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

如果 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.

注意

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

適用於

DownloadDataAsync(Uri, Object)

以非同步作業的方式,從指定的 URI 下載資源做為 Byte 陣列。Downloads the resource as a Byte array from the URI specified as an asynchronous operation.

public:
 void DownloadDataAsync(Uri ^ address, System::Object ^ userToken);
public void DownloadDataAsync (Uri address, object? userToken);
public void DownloadDataAsync (Uri address, object userToken);
member this.DownloadDataAsync : Uri * obj -> unit
Public Sub DownloadDataAsync (address As Uri, userToken As Object)

參數

address
Uri

Uri,包含要下載的 URI。A Uri containing the URI to download.

userToken
Object

使用者定義的物件,這個物件會在非同步作業完成時傳遞至叫用的方法。A user-defined object that is passed to the method invoked when the asynchronous operation completes.

例外狀況

address 參數為 nullThe address parameter is null.

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

-或--or-

下載資源時發生錯誤。An error occurred while downloading the resource.

備註

這個方法會使用與指定之 URI 配置相關聯之通訊協定的預設方法,來抓取指定的資源 addressThis method retrieves the specified resource using the default method for the protocol associated with the URI scheme specified in address. 系統會使用從執行緒集區自動設定的執行緒資源,以非同步方式下載資料。The data is downloaded asynchronously using thread resources that are automatically allocated from the thread pool.

在下載資源時,此方法不會封鎖呼叫執行緒。This method does not block the calling thread while downloading the resource. 若要在等候伺服器的回應時下載資源和封鎖,請使用其中一種 DownloadData 方法。To download a resource and block while waiting for the server's response, use one of the DownloadData methods. 當下載完成時, DownloadDataCompleted 就會引發事件。When the download completes, the DownloadDataCompleted event is raised. 您的應用程式必須處理此事件,才會收到通知。Your application must handle this event to receive notification. 已下載的資料會出現在 Result 屬性中。The downloaded data is available in the Result property.

在 .NET Framework 和 .NET Core 1.0 中,您可以藉由呼叫方法來取消尚未完成的非同步作業 CancelAsyncIn .NET Framework and .NET Core 1.0, you can cancel asynchronous operations that have not completed by calling the CancelAsync method.

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

如果 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.

注意

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

適用於