WebClient Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

Inheritance Hierarchy

System..::.Object
System.Net..::.WebClient

Namespace: System.Net
Assembly: System.Net (in System.Net.dll)

Syntax

Public Class WebClient
public class WebClient

The WebClient type exposes the following members.

Constructors

Name Description
WebClient Initializes a new instance of the WebClient class.

Top

Properties

Name Description
AllowReadStreamBuffering Gets or sets a value that indicates whether to buffer the data read from the Internet resource for a WebClient instance.
AllowWriteStreamBuffering Gets or sets a value that indicates whether to buffer the data written to the Internet resource for a WebClient instance.
BaseAddress Gets or sets the base URI for requests made by a WebClient.
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.
IsBusy Gets a value that indicates whether a Web request is in progress.
ResponseHeaders Gets a collection of header name/value pairs associated with the response.
UseDefaultCredentials Gets or sets a Boolean value that controls whether default credentials are sent with requests.

Top

Methods

Name Description
CancelAsync Cancels a pending asynchronous operation.
DownloadStringAsync(Uri) Downloads the resource at the specified Uri as a string.
DownloadStringAsync(Uri, Object) Downloads the resource at the specified Uri as a string.
Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
GetType Gets the Type of the current instance. (Inherited from Object.)
GetWebRequest Returns a WebRequest object for the specified resource.
GetWebResponse Returns the WebResponse for the specified WebRequest using the specified IAsyncResult.
MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
OnDownloadProgressChanged Raises the DownloadProgressChanged event.
OnDownloadStringCompleted Raises the DownloadStringCompleted event.
OnOpenReadCompleted Raises the OpenReadCompleted event.
OnOpenWriteCompleted Raises the OpenWriteCompleted event.
OnUploadProgressChanged Raises the UploadProgressChanged event.
OnUploadStringCompleted Raises the UploadStringCompleted event.
OnWriteStreamClosed Raises the WriteStreamClosed event.
OpenReadAsync(Uri) Opens a readable stream to the specified resource.
OpenReadAsync(Uri, Object) Opens a readable stream to the specified resource.
OpenWriteAsync(Uri) Opens a stream for writing data to the specified resource. This method does not block the calling thread.
OpenWriteAsync(Uri, String) Opens a stream for writing data to the specified resource. This method does not block the calling thread.
OpenWriteAsync(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.
ToString Returns a string that represents the current object. (Inherited from Object.)
UploadStringAsync(Uri, String) Uploads the specified string to the specified resource. This method does not block the calling thread.
UploadStringAsync(Uri, String, String) Uploads the specified string to the specified resource. This method does not block the calling thread.
UploadStringAsync(Uri, String, String, Object) Uploads the specified string to the specified resource. This method does not block the calling thread.

Top

Events

Name Description
DownloadProgressChanged Occurs when an asynchronous download operation successfully transfers some or all of the data.
DownloadStringCompleted Occurs when an asynchronous resource-download operation is completed.
OpenReadCompleted Occurs when an asynchronous resource-read operation is completed.
OpenWriteCompleted Occurs when an asynchronous operation to open a stream to write data to a resource completes.
UploadProgressChanged Occurs when an asynchronous upload operation successfully transfers some or all of the data.
UploadStringCompleted Occurs when an asynchronous string-upload operation completes.
WriteStreamClosed Occurs when an asynchronous write stream operation completes.

Top

Remarks

The WebClient class uses the WebRequest class to provide access to resources.

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

Method

Description

OpenWriteAsync

Retrieves a Stream used to send data to the resource asynchronously, 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

DownloadStringAsync

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

OpenReadAsync

Returns the data from a resource asynchronously, 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.

Some headers are considered restricted and are either exposed directly (such as Content-Type) or protected by the system and cannot be set in a WebHeaderCollection object. Any attempt to set one of these restricted headers in the WebHeaderCollection object using the Item[([(HttpRequestHeader])]) or Item[([(String])]) property or throws an exception. If the WebHeaderCollection object is associated with a HttpWebRequest object, the exception is thrown by the Item[([(HttpRequestHeader])]) property. If the WebHeaderCollection object is associated with a WebClient object, the exception is thrown when an attempt to send the WebClient request occurs.

For a list of restricted headers, see the Remarks in the WebHeaderCollection class.

Cross-domain connectivity allows an application to access resources from locations other than the site of origin. This is an important feature for enabling applications to consume existing services on the web. The security policy system requires that a policy file be downloaded from a network resource before a network connection is allowed access to that resource. This security policy system affects cross-domain network access for WebClient and HTTP classes in the System.Net namespace. Network connections for WebClient and HTTP classes to the site or host of origin do not require a security policy.

For security reasons, access is restricted to certain classes of URLs from the WebClient and HTTP classes in the System.Net namespace. There are similar access restrictions applied to other classes including the Image and MediaElement classes in the System.Windows.Controls namespace. Access restrictions also apply to XAML source files and font files based on the class of URL.

The connections affected are access to cross-zone, cross-domain, and cross-scheme URLs. These restrictions are designed to prevent networking threats.

For security reasons, the WebClient class is restricted from sending specific headers to a cross-domain site unless the header is allowed by the security policy applicable to the target cross-domain site. This restriction applies to resources from locations other than the site of origin. The Authorization header can be set using the Headers property. However to set the credentials properly, the cross-domain policy applicable to the target must have the http-request-headers set to allow the Authorization header to be sent.

Capabilities

If you use this API in your app, you must specify the following capabilities in the app manifest. Otherwise, your app might not work correctly or it might exit unexpectedly.

ID_CAP_NETWORKING

Windows Phone 8, Windows Phone OS 7.1

For more info, see App capabilities and hardware requirements for Windows Phone 8.

Version Notes

Windows Phone

Six active Web service connections are allowed simultaneously. Additional requests are paused until a connection is available.

Notes to Inheritors

Derived classes should call the base class implementation of WebClient to ensure the derived class works as expected.

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

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.

See Also

Reference

System.Net Namespace