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
注解
重要
不建议使用 WebClient
类进行新开发。 请改用 System.Net.Http.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 |
获取包含 IContainer 的 Component。 (继承自 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 |
已过时.
在使用写入流将数据写入资源的异步操作结束时发生。 |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈