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,则必须是一个相对 URI,address该 URI 必须与该 URI 组合在一起BaseAddress,以形成所请求数据的绝对 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,则必须是一个相对 URI,address该 URI 必须与该 URI 组合在一起BaseAddress,以形成所请求数据的绝对 URI。 QueryString如果该属性不是空字符串,则追加到 address

此方法使用 RETR 命令下载 FTP 资源。 对于 HTTP 资源,将使用 GET 方法。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅.NET Framework中的网络跟踪

适用于