WebClient 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供用于向 URI 标识的资源发送数据并从中接收数据的常见方法。
public ref class WebClient : System::ComponentModel::Component
public ref class WebClient sealed : System::ComponentModel::Component
public class WebClient : System.ComponentModel.Component
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class WebClient : System.ComponentModel.Component
[System.Runtime.InteropServices.ComVisible(true)]
public class WebClient : System.ComponentModel.Component
type WebClient = class
inherit Component
[<System.Runtime.InteropServices.ComVisible(true)>]
type WebClient = class
inherit Component
Public Class WebClient
Inherits Component
Public NotInheritable Class WebClient
Inherits Component
- 继承
- 属性
示例
下面的代码示例采用资源的 URI、检索资源并显示响应。
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::IO;
int main()
{
array<String^>^args = Environment::GetCommandLineArgs();
if ( args == nullptr || args->Length == 1 )
{
throw gcnew ApplicationException( "Specify the URI of the resource to retrieve." );
}
WebClient^ client = gcnew WebClient;
// Add a user agent header in case the
// requested URI contains a query.
client->Headers->Add( "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" );
Stream^ data = client->OpenRead( args[ 1 ] );
StreamReader^ reader = gcnew StreamReader( data );
String^ s = reader->ReadToEnd();
Console::WriteLine( s );
data->Close();
reader->Close();
delete client;
}
using System;
using System.Net;
using System.IO;
public class Test
{
public static void Main(string[] args)
{
if (args == null || args.Length == 0)
{
throw new ApplicationException("Specify the URI of the resource to retrieve.");
}
using WebClient client = new WebClient();
// Add a user agent header in case the
// requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
using Stream data = client.OpenRead(args[0]);
using StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine(s);
}
}
Imports System.Net
Imports System.IO
Public Class Test
Public Shared Sub Main(args() As String)
If args Is Nothing OrElse args.Length = 0 Then
Throw New ApplicationException("Specify the URI of the resource to retrieve.")
End If
Using client As New WebClient()
' Add a user agent header in case the
' requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
Using data As Stream = client.OpenRead(args(0))
Using reader As New StreamReader(data)
Dim s As String = reader.ReadToEnd()
Console.WriteLine(s)
End Using
End Using
End Using
End Sub
End Class
注解
谨慎
WebRequest、HttpWebRequest、ServicePoint和 WebClient 已过时,不应将其用于新开发。 请改用 HttpClient。
WebClient 类提供了一些常见方法,用于将数据发送到或接收 URI 标识的任何本地、Intranet 或 Internet 资源中的数据。
WebClient 类使用 WebRequest 类提供对资源的访问。 WebClient 实例可以使用注册到 WebRequest.RegisterPrefix 方法的任何 WebRequest 后代访问数据。
注意
默认情况下,.NET Framework 支持以 http:、https:、ftp:和 file: 方案标识符开头的 URI。
下表介绍了将数据上传到资源的 WebClient 方法。
| 方法 | 描述 |
|---|---|
| OpenWrite | 检索用于将数据发送到资源的 Stream。 |
| OpenWriteAsync | 检索用于将数据发送到资源的 Stream,而不会阻止调用线程。 |
| UploadData | 将字节数组发送到资源,并返回包含任何响应的 Byte 数组。 |
| UploadDataAsync | 将 Byte 数组发送到资源,而不会阻止调用线程。 |
| UploadFile | 将本地文件发送到资源,并返回包含任何响应的 Byte 数组。 |
| UploadFileAsync | 将本地文件发送到资源,而不会阻止调用线程。 |
| UploadValues | 将 NameValueCollection 发送到资源,并返回包含任何响应的 Byte 数组。 |
| UploadValuesAsync | 将 NameValueCollection 发送到资源,并返回包含任何响应的 Byte 数组,而不会阻止调用线程。 |
| UploadString | 将 String 发送到资源,并返回包含任何响应的 String。 |
| UploadStringAsync | 将 String 发送到资源,而不会阻止调用线程。 |
下表介绍了从资源下载数据的 WebClient 方法。
| 方法 | 描述 |
|---|---|
| OpenRead | 以 Stream的形式返回资源中的数据。 |
| OpenReadAsync | 从资源返回数据,而不阻止调用线程。 |
| DownloadData | 从资源下载数据并返回 Byte 数组。 |
| DownloadDataAsync | 从资源下载数据并返回 Byte 数组,而不会阻止调用线程。 |
| DownloadFile | 将数据从资源下载到本地文件。 |
| DownloadFileAsync | 将数据从资源下载到本地文件,而不会阻止调用线程。 |
| DownloadString | 从资源下载 String 并返回 String。 |
| DownloadStringAsync | 从资源下载 String,而不会阻止调用线程。 |
可以使用 CancelAsync 方法尝试取消异步操作。
默认情况下,WebClient 实例不会发送可选的 HTTP 标头。 如果请求需要可选标头,则必须将标头添加到 Headers 集合。 例如,若要在响应中保留查询,必须添加用户代理标头。 此外,如果缺少用户代理标头,服务器可能会返回 500(内部服务器错误)。
AllowAutoRedirect 设置为 WebClient 实例中的 true。
继承者说明
派生类应调用 WebClient 的基类实现,以确保派生类按预期工作。
构造函数
| WebClient() |
已过时.
初始化 WebClient 类的新实例。 |
属性
| AllowReadStreamBuffering |
已过时.
获取或设置一个值,该值指示是否缓冲从 WebClient 实例的 Internet 资源读取的数据。 |
| AllowWriteStreamBuffering |
已过时.
获取或设置一个值,该值指示是否缓冲写入 WebClient 实例的 Internet 资源的数据。 |
| BaseAddress |
获取或设置由 WebClient发出的请求的基本 URI。 |
| CachePolicy |
获取或设置此 WebClient 实例使用 WebRequest 对象获取的任何资源的应用程序的缓存策略。 |
| CanRaiseEvents |
获取一个值,该值指示组件是否可以引发事件。 (继承自 Component) |
| Container |
获取包含 Component的 IContainer。 (继承自 Component) |
| Credentials |
获取或设置发送到主机并用于对请求进行身份验证的网络凭据。 |
| DesignMode |
获取一个值,该值指示 Component 当前是否处于设计模式。 (继承自 Component) |
| Encoding |
获取或设置用于上传和下载字符串的 Encoding。 |
| Events |
获取附加到此 Component的事件处理程序的列表。 (继承自 Component) |
| Headers |
获取或设置与请求关联的标头名称/值对的集合。 |
| IsBusy |
获取 Web 请求是否正在进行。 |
| Proxy |
获取或设置此 WebClient 对象使用的代理。 |
| QueryString |
获取或设置与请求关联的查询名称/值对的集合。 |
| ResponseHeaders |
获取与响应关联的标头名称/值对的集合。 |
| Site | (继承自 Component) |
| UseDefaultCredentials |
获取或设置一个 Boolean 值,该值控制是否使用请求发送 DefaultCredentials。 |
方法
事件
| Disposed |
当组件通过调用 Dispose() 方法释放时发生。 (继承自 Component) |
| DownloadDataCompleted |
异步数据下载操作完成时发生。 |
| DownloadFileCompleted |
异步文件下载操作完成时发生。 |
| DownloadProgressChanged |
当异步下载操作成功传输部分或所有数据时发生。 |
| DownloadStringCompleted |
异步资源下载操作完成时发生。 |
| OpenReadCompleted |
当异步操作打开包含资源的流完成时发生。 |
| OpenWriteCompleted |
在打开流以将数据写入资源完成的异步操作时发生。 |
| UploadDataCompleted |
异步数据上传操作完成时发生。 |
| UploadFileCompleted |
异步文件上传操作完成时发生。 |
| UploadProgressChanged |
异步上传操作成功传输部分或所有数据时发生。 |
| UploadStringCompleted |
异步字符串上传操作完成时发生。 |
| UploadValuesCompleted |
当名称/值集合的异步上传完成时发生。 |
| WriteStreamClosed |
已过时.
当使用写入流将数据写入资源的异步操作关闭时发生。 |