This documentation is archived and is not being maintained.

WebClient Class

Provides common methods for sending data to and receiving data from a resource identified by a URI. This class cannot be inherited.

For a list of all members of this type, see WebClient Members.


[Visual Basic]
NotInheritable Public Class WebClient
   Inherits Component
public sealed class WebClient : Component
public __gc __sealed class WebClient : public Component
class WebClient extends Component

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


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 Internet resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest.RegisterPrefix method.

Note   By default, the .NET Framework supports URIs that begin with http:, https:, and file: scheme identifiers.

The WebClient class provides four methods for uploading data to a resource:

  • OpenWrite returns a Stream used to send data to the resource.
  • UploadData sends a byte array to the resource and returns a byte array containing any response.
  • UploadFile sends a local file to the resource and returns a byte array containing any response.
  • UploadValues sends a NameValueCollection to the resource and returns a byte array containing any response.

The WebClient class also provides three methods for downloading data from a resource:

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.


[Visual Basic, C#] The following example takes the URI of a resource, retrieves it, and displays the response.

[Visual Basic] 
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()
    End Sub 'Main
End Class 'Test

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 ();

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Net

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

.NET Framework Security: 

  • WebPermission to access the requested URI or any URI that the request is redirected to. Associated enumeration: Connect.

See Also

WebClient Members | System.Net Namespace | WebRequest | WebResponse | HttpWebRequest | HttpWebResponse