WebClient.DownloadData 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从指定的 URI 将资源下载为 Byte 数组。
重载
| DownloadData(String) |
从指定的 URI 将资源下载为 Byte 数组。 |
| DownloadData(Uri) |
从指定的 URI 将资源下载为 Byte 数组。 |
DownloadData(String)
- Source:
- WebClient.cs
- Source:
- WebClient.cs
- Source:
- WebClient.cs
- Source:
- 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)
- Source:
- WebClient.cs
- Source:
- WebClient.cs
- Source:
- WebClient.cs
- Source:
- 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中的