WebClient Class
Assembly: System (in system.dll)
The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI.
The WebClient class uses the WebRequest class to provide access to resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest.RegisterPrefix method.
![]() |
---|
By default, the .NET Framework supports URIs that begin with http:, https:, ftp:, and file: scheme identifiers. |
The following table describes WebClient methods for uploading data to a resource.
Method | Description |
---|---|
Retrieves a Stream used to send data to the resource. | |
Retrieves a Stream used to send data to the resource, without blocking the calling thread. | |
Sends a byte array to the resource and returns a Byte array containing any response. | |
Sends a Byte array to the resource, without blocking the calling thread. | |
Sends a local file to the resource and returns a Byte array containing any response. | |
Sends a local file to the resource, without blocking the calling thread. | |
Sends a NameValueCollection to the resource and returns a Byte array containing any response. | |
Sends a NameValueCollection to the resource and returns a Byte array containing any response, without blocking the calling thread. | |
Sends a String to the resource, without blocking the calling thread. | |
Sends a String to the resource, without blocking the calling thread. |
The following table describes WebClient methods for downloading data from a resource.
Method | Description |
---|---|
Returns the data from a resource as a Stream. | |
Returns the data from a resource, without blocking the calling thread. | |
Downloads data from a resource and returns a Byte array. | |
Downloads data from a resource and returns a Byte array, without blocking the calling thread. | |
Downloads data from a resource to a local file. | |
Downloads data from a resource to a local file, without blocking the calling thread. | |
Downloads a String from a resource and returns a String. | |
Downloads a String from a resource, without blocking the calling thread. |
You can use the CancelAsync method to cancel asynchronous operations that have not completed.
A WebClient instance does not send optional HTTP headers by default. If your request requires an optional header, you must add the header to the Headers collection. For example, to retain queries in the response, you must add a user-agent header. Also, servers may return 500 (Internal Server Error) if the user agent header is missing.
AllowAutoRedirect is set to true in WebClient instances.
Notes to Inheritors: Derived classes should call the base class implementation of WebClient to ensure the derived class works as expected.The following code example takes the URI of a resource, retrieves it, and displays the response.
Imports System 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 'Main End Class 'Test
- WebPermission to access the requested URI or any URI that the request is redirected to. Associated enumeration: Connect.
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.