WebClient Class

Definition

Provides common methods for sending data to and receiving data from a resource identified by a URI.

[System.Runtime.InteropServices.ComVisible(true)]
public class WebClient : System.ComponentModel.Component
Inheritance
Attributes

Inherited Members

System.ComponentModel.Component

System.MarshalByRefObject

System.Object

Examples

The following code example takes the URI of a resource, retrieves it, and displays the response.

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

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
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

Remarks

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.

Note

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
OpenWrite Retrieves a Stream used to send data to the resource.
OpenWriteAsync Retrieves a Stream used to send data to the resource, without blocking the calling thread.
UploadData Sends a byte array to the resource and returns a Byte array containing any response.
UploadDataAsync Sends a Byte array to the resource, without blocking the calling thread.
UploadFile Sends a local file to the resource and returns a Byte array containing any response.
UploadFileAsync Sends a local file to the resource, without blocking the calling thread.
UploadValues Sends a NameValueCollection to the resource and returns a Byte array containing any response.
UploadValuesAsync Sends a NameValueCollection to the resource and returns a Byte array containing any response, without blocking the calling thread.
UploadString Sends a String to the resource, without blocking the calling thread.
UploadStringAsync 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
OpenRead Returns the data from a resource as a Stream.
OpenReadAsync Returns the data from a resource, without blocking the calling thread.
DownloadData Downloads data from a resource and returns a Byte array.
DownloadDataAsync Downloads data from a resource and returns a Byte array, without blocking the calling thread.
DownloadFile Downloads data from a resource to a local file.
DownloadFileAsync Downloads data from a resource to a local file, without blocking the calling thread.
DownloadString Downloads a String from a resource and returns a String.
DownloadStringAsync 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.

Constructors

Web<wbr>Client()

Initializes a new instance of the WebClient class.

Properties

Allow<wbr>Read<wbr>Stream<wbr>Buffering

Gets or sets a value that indicates whether to buffer the data read from the Internet resource for a WebClient instance.

Allow<wbr>Write<wbr>Stream<wbr>Buffering

Gets or sets a value that indicates whether to buffer the data written to the Internet resource for a WebClient instance.

Base<wbr>Address

Gets or sets the base URI for requests made by a WebClient.

Cache<wbr>Policy

Gets or sets the application's cache policy for any resources obtained by this WebClient instance using WebRequest objects.

Credentials

Gets or sets the network credentials that are sent to the host and used to authenticate the request.

Encoding

Gets and sets the Encoding used to upload and download strings.

Headers

Gets or sets a collection of header name/value pairs associated with the request.

Is<wbr>Busy

Gets whether a Web request is in progress.

Proxy

Gets or sets the proxy used by this WebClient object.

Query<wbr>String

Gets or sets a collection of query name/value pairs associated with the request.

Response<wbr>Headers

Gets a collection of header name/value pairs associated with the response.

Use<wbr>Default<wbr>Credentials

Gets or sets a Boolean value that controls whether the DefaultCredentials are sent with requests.

Methods

Cancel<wbr>Async()

Cancels a pending asynchronous operation.

Download<wbr>Data(String)

Downloads the resource as a Byte array from the URI specified.

Download<wbr>Data(Uri)

Downloads the resource as a Byte array from the URI specified.

Download<wbr>Data<wbr>Async(Uri)

Downloads the resource as a Byte array from the URI specified as an asynchronous operation.

Download<wbr>Data<wbr>Async(Uri, Object)

Downloads the resource as a Byte array from the URI specified as an asynchronous operation.

Download<wbr>Data<wbr>Task<wbr>Async(String)

Downloads the resource as a Byte array from the URI specified as an asynchronous operation using a task object.

Download<wbr>Data<wbr>Task<wbr>Async(Uri)

Downloads the resource as a Byte array from the URI specified as an asynchronous operation using a task object.

Download<wbr>File(Uri, String)

Downloads the resource with the specified URI to a local file.

Download<wbr>File(String, String)

Downloads the resource with the specified URI to a local file.

Download<wbr>File<wbr>Async(Uri, String)

Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread.

Download<wbr>File<wbr>Async(Uri, String, Object)

Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread.

Download<wbr>File<wbr>Task<wbr>Async(String, String)

Downloads the specified resource to a local file as an asynchronous operation using a task object.

Download<wbr>File<wbr>Task<wbr>Async(Uri, String)

Downloads the specified resource to a local file as an asynchronous operation using a task object.

Download<wbr>String(String)

Downloads the requested resource as a String. The resource to download is specified as a String containing the URI.

Download<wbr>String(Uri)

Downloads the requested resource as a String. The resource to download is specified as a Uri.

Download<wbr>String<wbr>Async(Uri)

Downloads the resource specified as a Uri. This method does not block the calling thread.

Download<wbr>String<wbr>Async(Uri, Object)

Downloads the specified string to the specified resource. This method does not block the calling thread.

Download<wbr>String<wbr>Task<wbr>Async(String)

Downloads the resource as a String from the URI specified as an asynchronous operation using a task object.

Download<wbr>String<wbr>Task<wbr>Async(Uri)

Downloads the resource as a String from the URI specified as an asynchronous operation using a task object.

Get<wbr>Web<wbr>Request(Uri)

Returns a WebRequest object for the specified resource.

Get<wbr>Web<wbr>Response(Web<wbr>Request)

Returns the WebResponse for the specified WebRequest.

Get<wbr>Web<wbr>Response(Web<wbr>Request, IAsync<wbr>Result)

Returns the WebResponse for the specified WebRequest using the specified IAsyncResult.

On<wbr>Download<wbr>Data<wbr>Completed(Download<wbr>Data<wbr>Completed<wbr>Event<wbr>Args)

Raises the DownloadDataCompleted event.

On<wbr>Download<wbr>File<wbr>Completed(Async<wbr>Completed<wbr>Event<wbr>Args)

Raises the DownloadFileCompleted event.

On<wbr>Download<wbr>Progress<wbr>Changed(Download<wbr>Progress<wbr>Changed<wbr>Event<wbr>Args)

Raises the DownloadProgressChanged event.

On<wbr>Download<wbr>String<wbr>Completed(Download<wbr>String<wbr>Completed<wbr>Event<wbr>Args)

Raises the DownloadStringCompleted event.

On<wbr>Open<wbr>Read<wbr>Completed(Open<wbr>Read<wbr>Completed<wbr>Event<wbr>Args)

Raises the OpenReadCompleted event.

On<wbr>Open<wbr>Write<wbr>Completed(Open<wbr>Write<wbr>Completed<wbr>Event<wbr>Args)

Raises the OpenWriteCompleted event.

On<wbr>Upload<wbr>Data<wbr>Completed(Upload<wbr>Data<wbr>Completed<wbr>Event<wbr>Args)

Raises the UploadDataCompleted event.

On<wbr>Upload<wbr>File<wbr>Completed(Upload<wbr>File<wbr>Completed<wbr>Event<wbr>Args)

Raises the UploadFileCompleted event.

On<wbr>Upload<wbr>Progress<wbr>Changed(Upload<wbr>Progress<wbr>Changed<wbr>Event<wbr>Args)

Raises the UploadProgressChanged event.

On<wbr>Upload<wbr>String<wbr>Completed(Upload<wbr>String<wbr>Completed<wbr>Event<wbr>Args)

Raises the UploadStringCompleted event.

On<wbr>Upload<wbr>Values<wbr>Completed(Upload<wbr>Values<wbr>Completed<wbr>Event<wbr>Args)

Raises the UploadValuesCompleted event.

On<wbr>Write<wbr>Stream<wbr>Closed(Write<wbr>Stream<wbr>Closed<wbr>Event<wbr>Args)

Raises the WriteStreamClosed event.

Open<wbr>Read(String)

Opens a readable stream for the data downloaded from a resource with the URI specified as a String.

Open<wbr>Read(Uri)

Opens a readable stream for the data downloaded from a resource with the URI specified as a Uri

Open<wbr>Read<wbr>Async(Uri)

Opens a readable stream containing the specified resource. This method does not block the calling thread.

Open<wbr>Read<wbr>Async(Uri, Object)

Opens a readable stream containing the specified resource. This method does not block the calling thread.

Open<wbr>Read<wbr>Task<wbr>Async(String)

Opens a readable stream containing the specified resource as an asynchronous operation using a task object.

Open<wbr>Read<wbr>Task<wbr>Async(Uri)

Opens a readable stream containing the specified resource as an asynchronous operation using a task object.

Open<wbr>Write(String)

Opens a stream for writing data to the specified resource.

Open<wbr>Write(Uri)

Opens a stream for writing data to the specified resource.

Open<wbr>Write(String, String)

Opens a stream for writing data to the specified resource, using the specified method.

Open<wbr>Write(Uri, String)

Opens a stream for writing data to the specified resource, by using the specified method.

Open<wbr>Write<wbr>Async(Uri, String, Object)

Opens a stream for writing data to the specified resource, using the specified method. This method does not block the calling thread.

Open<wbr>Write<wbr>Async(Uri, String)

Opens a stream for writing data to the specified resource. This method does not block the calling thread.

Open<wbr>Write<wbr>Async(Uri)

Opens a stream for writing data to the specified resource. This method does not block the calling thread.

Open<wbr>Write<wbr>Task<wbr>Async(String)

Opens a stream for writing data to the specified resource as an asynchronous operation using a task object.

Open<wbr>Write<wbr>Task<wbr>Async(Uri)

Opens a stream for writing data to the specified resource as an asynchronous operation using a task object.

Open<wbr>Write<wbr>Task<wbr>Async(String, String)

Opens a stream for writing data to the specified resource as an asynchronous operation using a task object.

Open<wbr>Write<wbr>Task<wbr>Async(Uri, String)

Opens a stream for writing data to the specified resource as an asynchronous operation using a task object.

Upload<wbr>Data(String, Byte[])

Uploads a data buffer to a resource identified by a URI.

Upload<wbr>Data(Uri, Byte[])

Uploads a data buffer to a resource identified by a URI.

Upload<wbr>Data(String, String, Byte[])

Uploads a data buffer to the specified resource, using the specified method.

Upload<wbr>Data(Uri, String, Byte[])

Uploads a data buffer to the specified resource, using the specified method.

Upload<wbr>Data<wbr>Async(Uri, String, Byte[])

Uploads a data buffer to a resource identified by a URI, using the specified method. This method does not block the calling thread.

Upload<wbr>Data<wbr>Async(Uri, String, Byte[], Object)

Uploads a data buffer to a resource identified by a URI, using the specified method and identifying token.

Upload<wbr>Data<wbr>Async(Uri, Byte[])

Uploads a data buffer to a resource identified by a URI, using the POST method. This method does not block the calling thread.

Upload<wbr>Data<wbr>Task<wbr>Async(String, Byte[])

Uploads a data buffer that contains a Byte array to the URI specified as an asynchronous operation using a task object.

Upload<wbr>Data<wbr>Task<wbr>Async(Uri, Byte[])

Uploads a data buffer that contains a Byte array to the URI specified as an asynchronous operation using a task object.

Upload<wbr>Data<wbr>Task<wbr>Async(String, String, Byte[])

Uploads a data buffer that contains a Byte array to the URI specified as an asynchronous operation using a task object.

Upload<wbr>Data<wbr>Task<wbr>Async(Uri, String, Byte[])

Uploads a data buffer that contains a Byte array to the URI specified as an asynchronous operation using a task object.

Upload<wbr>File(String, String)

Uploads the specified local file to a resource with the specified URI.

Upload<wbr>File(Uri, String)

Uploads the specified local file to a resource with the specified URI.

Upload<wbr>File(String, String, String)

Uploads the specified local file to the specified resource, using the specified method.

Upload<wbr>File(Uri, String, String)

Uploads the specified local file to the specified resource, using the specified method.

Upload<wbr>File<wbr>Async(Uri, String)

Uploads the specified local file to the specified resource, using the POST method. This method does not block the calling thread.

Upload<wbr>File<wbr>Async(Uri, String, String)

Uploads the specified local file to the specified resource, using the POST method. This method does not block the calling thread.

Upload<wbr>File<wbr>Async(Uri, String, String, Object)

Uploads the specified local file to the specified resource, using the POST method. This method does not block the calling thread.

Upload<wbr>File<wbr>Task<wbr>Async(Uri, String, String)

Uploads the specified local file to a resource as an asynchronous operation using a task object.

Upload<wbr>File<wbr>Task<wbr>Async(String, String, String)

Uploads the specified local file to a resource as an asynchronous operation using a task object.

Upload<wbr>File<wbr>Task<wbr>Async(String, String)

Uploads the specified local file to a resource as an asynchronous operation using a task object.

Upload<wbr>File<wbr>Task<wbr>Async(Uri, String)

Uploads the specified local file to a resource as an asynchronous operation using a task object.

Upload<wbr>String(String, String)

Uploads the specified string to the specified resource, using the POST method.

Upload<wbr>String(Uri, String)

Uploads the specified string to the specified resource, using the POST method.

Upload<wbr>String(String, String, String)

Uploads the specified string to the specified resource, using the specified method.

Upload<wbr>String(Uri, String, String)

Uploads the specified string to the specified resource, using the specified method.

Upload<wbr>String<wbr>Async(Uri, String)

Uploads the specified string to the specified resource. This method does not block the calling thread.

Upload<wbr>String<wbr>Async(Uri, String, String)

Uploads the specified string to the specified resource. This method does not block the calling thread.

Upload<wbr>String<wbr>Async(Uri, String, String, Object)

Uploads the specified string to the specified resource. This method does not block the calling thread.

Upload<wbr>String<wbr>Task<wbr>Async(Uri, String, String)

Uploads the specified string to the specified resource as an asynchronous operation using a task object.

Upload<wbr>String<wbr>Task<wbr>Async(String, String, String)

Uploads the specified string to the specified resource as an asynchronous operation using a task object.

Upload<wbr>String<wbr>Task<wbr>Async(String, String)

Uploads the specified string to the specified resource as an asynchronous operation using a task object.

Upload<wbr>String<wbr>Task<wbr>Async(Uri, String)

Uploads the specified string to the specified resource as an asynchronous operation using a task object.

Upload<wbr>Values(String, Name<wbr>Value<wbr>Collection)

Uploads the specified name/value collection to the resource identified by the specified URI.

Upload<wbr>Values(Uri, Name<wbr>Value<wbr>Collection)

Uploads the specified name/value collection to the resource identified by the specified URI.

Upload<wbr>Values(String, String, Name<wbr>Value<wbr>Collection)

Uploads the specified name/value collection to the resource identified by the specified URI, using the specified method.

Upload<wbr>Values(Uri, String, Name<wbr>Value<wbr>Collection)

Uploads the specified name/value collection to the resource identified by the specified URI, using the specified method.

Upload<wbr>Values<wbr>Async(Uri, Name<wbr>Value<wbr>Collection)

Uploads the data in the specified name/value collection to the resource identified by the specified URI. This method does not block the calling thread.

Upload<wbr>Values<wbr>Async(Uri, String, Name<wbr>Value<wbr>Collection)

Uploads the data in the specified name/value collection to the resource identified by the specified URI, using the specified method. This method does not block the calling thread.

Upload<wbr>Values<wbr>Async(Uri, String, Name<wbr>Value<wbr>Collection, Object)

Uploads the data in the specified name/value collection to the resource identified by the specified URI, using the specified method. This method does not block the calling thread, and allows the caller to pass an object to the method that is invoked when the operation completes.

Upload<wbr>Values<wbr>Task<wbr>Async(String, String, Name<wbr>Value<wbr>Collection)

Uploads the specified name/value collection to the resource identified by the specified URI as an asynchronous operation using a task object.

Upload<wbr>Values<wbr>Task<wbr>Async(String, Name<wbr>Value<wbr>Collection)

Uploads the specified name/value collection to the resource identified by the specified URI as an asynchronous operation using a task object.

Upload<wbr>Values<wbr>Task<wbr>Async(Uri, Name<wbr>Value<wbr>Collection)

Uploads the specified name/value collection to the resource identified by the specified URI as an asynchronous operation using a task object.

Upload<wbr>Values<wbr>Task<wbr>Async(Uri, String, Name<wbr>Value<wbr>Collection)

Uploads the specified name/value collection to the resource identified by the specified URI as an asynchronous operation using a task object.

Events

Download<wbr>Data<wbr>Completed

Occurs when an asynchronous data download operation completes.

Download<wbr>File<wbr>Completed

Occurs when an asynchronous file download operation completes.

Download<wbr>Progress<wbr>Changed

Occurs when an asynchronous download operation successfully transfers some or all of the data.

Download<wbr>String<wbr>Completed

Occurs when an asynchronous resource-download operation completes.

Open<wbr>Read<wbr>Completed

Occurs when an asynchronous operation to open a stream containing a resource completes.

Open<wbr>Write<wbr>Completed

Occurs when an asynchronous operation to open a stream to write data to a resource completes.

Upload<wbr>Data<wbr>Completed

Occurs when an asynchronous data-upload operation completes.

Upload<wbr>File<wbr>Completed

Occurs when an asynchronous file-upload operation completes.

Upload<wbr>Progress<wbr>Changed

Occurs when an asynchronous upload operation successfully transfers some or all of the data.

Upload<wbr>String<wbr>Completed

Occurs when an asynchronous string-upload operation completes.

Upload<wbr>Values<wbr>Completed

Occurs when an asynchronous upload of a name/value collection completes.

Write<wbr>Stream<wbr>Closed

Occurs when an asynchronous operation to write data to a resource using a write stream is closed.