System.Net.WebClient Class

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

See Also: WebClient Members

Syntax

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

Remarks

The System.Net.WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI.

The System.Net.WebClient class uses the System.Net.WebRequest class to provide access to resources. System.Net.WebClient instances can access data with any System.Net.WebRequest descendant registered with the WebRequest.RegisterPrefix(string, IWebRequestCreate) method.

Note:

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

The following table describes System.Net.WebClient methods for uploading data to a resource.

WebClient.OpenWrite(string)

Retrieves a System.IO.Stream used to send data to the resource.

WebClient.OpenWriteAsync(Uri)

Retrieves a System.IO.Stream used to send data to the resource, without blocking the calling thread.

WebClient.UploadData(string, Byte[])

Sends a byte array to the resource and returns a byte array containing any response.

WebClient.UploadDataAsync(Uri, Byte[])

Sends a byte array to the resource, without blocking the calling thread.

WebClient.UploadFile(string, string)

Sends a local file to the resource and returns a byte array containing any response.

WebClient.UploadFileAsync(Uri, string)

Sends a local file to the resource, without blocking the calling thread.

WebClient.UploadValues(string, System.Collections.Specialized.NameValueCollection)

Sends a System.Collections.Specialized.NameValueCollection to the resource and returns a byte array containing any response.

WebClient.UploadValuesAsync(Uri, System.Collections.Specialized.NameValueCollection)

Sends a System.Collections.Specialized.NameValueCollection to the resource and returns a byte array containing any response, without blocking the calling thread.

erload:System.Net.WebClient.UploadString

Sends a string to the resource, without blocking the calling thread.

erload:System.Net.WebClient.UploadStringAsync

Sends a string to the resource, without blocking the calling thread.

The following table describes System.Net.WebClient methods for downloading data from a resource.

WebClient.OpenRead(string)

Returns the data from a resource as a System.IO.Stream.

WebClient.OpenReadAsync(Uri)

Returns the data from a resource, without blocking the calling thread.

WebClient.DownloadData(string)

Downloads data from a resource and returns a byte array.

WebClient.DownloadDataAsync(Uri)

Downloads data from a resource and returns a byte array, without blocking the calling thread.

WebClient.DownloadFile(string, string)

Downloads data from a resource to a local file.

WebClient.DownloadFileAsync(Uri, string)

Downloads data from a resource to a local file, without blocking the calling thread.

WebClient.DownloadString(string)

Downloads a string from a resource and returns a string.

erload:System.Net.WebClient.DownloadStringAsync

Downloads a string from a resource, without blocking the calling thread.

You can use the WebClient.CancelAsync method to cancel asynchronous operations that have not completed.

A System.Net.WebClient instance does not send optional HTTP headers by default. If your request requires an optional header, you must add the header to the WebClient.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.

HttpWebRequest.AllowAutoRedirect is set to true in System.Net.WebClient instances.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Requirements

Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.x.x, 1.0.5000.0, 2.0.0.0, 4.0.0.0