WebClient.DownloadData 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从指定 URI 下载资源作为 Byte 数组。
重载
DownloadData(String) |
从指定 URI 下载资源作为 Byte 数组。 |
DownloadData(Uri) |
从指定 URI 下载资源作为 Byte 数组。 |
DownloadData(String)
- 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.")
注解
方法 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
从指定 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
。
注解
方法 DownloadData 使用 参数指定的 address
URI 下载资源。 此方法在下载资源时阻止。 若要下载资源并在等待服务器的响应时继续执行,请使用 方法之 DownloadDataAsync 一。
BaseAddress如果 属性不是空字符串 (“”) ,并且address
不包含绝对 URI,address
则必须是与 BaseAddress 组合在一起以构成所请求数据的绝对 URI 的相对 URI。
QueryString如果 属性不是空字符串,则会将其追加到 address
。
此方法使用 RETR 命令下载 FTP 资源。 对于 HTTP 资源,使用 GET 方法。
备注
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪。
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈