WebClient.DownloadData 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從指定的 URI 下載資源作為 Byte 陣列。
多載
| 名稱 | Description |
|---|---|
| DownloadData(String) |
從指定的 URI 下載資源作為 Byte 陣列。 |
| DownloadData(Uri) |
從指定的 URI 下載資源作為 Byte 陣列。 |
DownloadData(String)
- 來源:
- WebClient.cs
- 來源:
- WebClient.cs
- 來源:
- WebClient.cs
- 來源:
- WebClient.cs
從指定的 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 陣列。
例外狀況
address 參數是 null。
方法已在多個線程上同時呼叫。
範例
下列程式代碼範例會向伺服器要求數據,並顯示傳回的數據。 它假設 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.")
備註
謹慎
WebRequest、HttpWebRequest、ServicePoint和 WebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient。
DownloadData 方法會使用 address 參數指定的 URI 下載資源。 此方法會在下載資源時封鎖。 若要下載資源,並在等候伺服器的回應時繼續執行,請使用其中一個 DownloadDataAsync 方法。
如果 BaseAddress 屬性不是空字串 (“”) 且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求數據的絕對 URI。 如果 QueryString 屬性不是空字串,則會附加至 address。
此方法會使用 RETR 命令來下載 FTP 資源。 針對 HTTP 資源,會使用 GET 方法。
注意
當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤。
適用於
DownloadData(Uri)
- 來源:
- WebClient.cs
- 來源:
- WebClient.cs
- 來源:
- WebClient.cs
- 來源:
- WebClient.cs
從指定的 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()
參數
傳回
包含下載資源的 Byte 陣列。
例外狀況
address 參數是 null。
備註
謹慎
WebRequest、HttpWebRequest、ServicePoint和 WebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient。
DownloadData 方法會使用 address 參數指定的 URI 下載資源。 此方法會在下載資源時封鎖。 若要下載資源,並在等候伺服器的回應時繼續執行,請使用其中一個 DownloadDataAsync 方法。
如果 BaseAddress 屬性不是空字串 (“”) 且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求數據的絕對 URI。 如果 QueryString 屬性不是空字串,則會附加至 address。
此方法會使用 RETR 命令來下載 FTP 資源。 針對 HTTP 資源,會使用 GET 方法。
注意
當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤。