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
WebClient
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();
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.");
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;)");
Stream data = client.OpenRead (args[0]);
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd ();
Console.WriteLine (s);
data.Close ();
reader.Close ();
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
Dim 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;)")
Dim data As Stream = client.OpenRead(args(0))
Dim reader As New StreamReader(data)
Dim s As String = reader.ReadToEnd()
Console.WriteLine(s)
data.Close()
reader.Close()
End Sub
End Class
不建议将类用于 WebClient
新开发。 请改用该 System.Net.Http.HttpClient 类。
此类 WebClient 提供用于将数据发送到或接收 URI 标识的任何本地、Intranet 或 Internet 资源的常见方法。
该 WebClient 类使用 WebRequest 该类提供对资源的访问。 WebClient实例可以访问使用方法注册WebRequest.RegisterPrefix的任何WebRequest后代的数据。
默认情况下,.NET Framework支持以、https:``ftp:
和file:
方案标识符开头http:
的 URI。
下表介绍了 WebClient 将数据上传到资源的方法。
可以使用该方法 CancelAsync 尝试取消异步操作。
WebClient默认情况下,实例不会发送可选的 HTTP 标头。 如果请求需要可选标头,则必须将 Headers 标头添加到集合中。 例如,若要在响应中保留查询,必须添加用户代理标头。 此外,如果缺少用户代理标头,服务器可能会返回 500 (内部服务器错误) 。
AllowAutoRedirect is set to true
in WebClient instances.
继承者说明
派生类应调用基类实现 WebClient ,以确保派生类按预期工作。