Export (0) Print
Expand All

WebClient Class

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

System.Object
  System.Net.WebClient

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

public class WebClient

The WebClient type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneWebClientInitializes a new instance of the WebClient class.
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneAllowReadStreamBufferingGets or sets a value that indicates whether to buffer the data read from the Internet resource for a WebClient instance.
Public propertySupported by Silverlight for Windows PhoneAllowWriteStreamBufferingGets or sets a value that indicates whether to buffer the data written to the Internet resource for a WebClient instance.
Public propertySupported by Silverlight for Windows PhoneBaseAddressGets or sets the base URI for requests made by a WebClient.
Public propertySupported by Silverlight for Windows PhoneCredentialsGets or sets the network credentials that are sent to the host and used to authenticate the request.
Public propertySupported by Silverlight for Windows PhoneEncodingGets and sets the Encoding used to upload and download strings.
Public propertySupported by Silverlight for Windows PhoneHeadersGets or sets a collection of header name/value pairs associated with the request.
Public propertySupported by Silverlight for Windows PhoneIsBusyGets a value that indicates whether a Web request is in progress.
Public propertySupported by Silverlight for Windows PhoneResponseHeadersGets a collection of header name/value pairs associated with the response.
Public propertySupported by Silverlight for Windows PhoneUseDefaultCredentialsGets or sets a Boolean value that controls whether default credentials are sent with requests.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneCancelAsyncCancels a pending asynchronous operation.
Public methodSupported by Silverlight for Windows PhoneDownloadStringAsync(Uri)Downloads the resource at the specified Uri as a string.
Public methodSupported by Silverlight for Windows PhoneDownloadStringAsync(Uri, Object)Downloads the resource at the specified Uri as a string.
Public methodSupported by Silverlight for Windows PhoneEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneGetWebRequestReturns a WebRequest object for the specified resource.
Protected methodSupported by Silverlight for Windows PhoneGetWebResponseReturns the WebResponse for the specified WebRequest using the specified IAsyncResult.
Protected methodSupported by Silverlight for Windows PhoneMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneOnDownloadProgressChangedRaises the DownloadProgressChanged event.
Protected methodSupported by Silverlight for Windows PhoneOnDownloadStringCompletedRaises the DownloadStringCompleted event.
Protected methodSupported by Silverlight for Windows PhoneOnOpenReadCompletedRaises the OpenReadCompleted event.
Protected methodSupported by Silverlight for Windows PhoneOnOpenWriteCompletedRaises the OpenWriteCompleted event.
Protected methodSupported by Silverlight for Windows PhoneOnUploadProgressChangedRaises the UploadProgressChanged event.
Protected methodSupported by Silverlight for Windows PhoneOnUploadStringCompletedRaises the UploadStringCompleted event.
Protected methodSupported by Silverlight for Windows PhoneOnWriteStreamClosedRaises the WriteStreamClosed event.
Public methodSupported by Silverlight for Windows PhoneOpenReadAsync(Uri)Opens a readable stream to the specified resource.
Public methodSupported by Silverlight for Windows PhoneOpenReadAsync(Uri, Object)Opens a readable stream to the specified resource.
Public methodSupported by Silverlight for Windows PhoneOpenWriteAsync(Uri)Opens a stream for writing data to the specified resource. This method does not block the calling thread.
Public methodSupported by Silverlight for Windows PhoneOpenWriteAsync(Uri, String)Opens a stream for writing data to the specified resource. This method does not block the calling thread.
Public methodSupported by Silverlight for Windows PhoneOpenWriteAsync(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.
Public methodSupported by Silverlight for Windows PhoneToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneUploadStringAsync(Uri, String)Uploads the specified string to the specified resource. This method does not block the calling thread.
Public methodSupported by Silverlight for Windows PhoneUploadStringAsync(Uri, String, String)Uploads the specified string to the specified resource. This method does not block the calling thread.
Public methodSupported by Silverlight for Windows PhoneUploadStringAsync(Uri, String, String, Object)Uploads the specified string to the specified resource. This method does not block the calling thread.
Top

  NameDescription
Public eventSupported by Silverlight for Windows PhoneDownloadProgressChangedOccurs when an asynchronous download operation successfully transfers some or all of the data.
Public eventSupported by Silverlight for Windows PhoneDownloadStringCompletedOccurs when an asynchronous resource-download operation is completed.
Public eventSupported by Silverlight for Windows PhoneOpenReadCompletedOccurs when an asynchronous resource-read operation is completed.
Public eventSupported by Silverlight for Windows PhoneOpenWriteCompletedOccurs when an asynchronous operation to open a stream to write data to a resource completes.
Public eventSupported by Silverlight for Windows PhoneUploadProgressChangedOccurs when an asynchronous upload operation successfully transfers some or all of the data.
Public eventSupported by Silverlight for Windows PhoneUploadStringCompletedOccurs when an asynchronous string-upload operation completes.
Public eventSupported by Silverlight for Windows PhoneWriteStreamClosedOccurs when an asynchronous write stream operation completes.
Top

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

You must use the WebClient class from a Silverlight control that is hosted in a Web page.

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.

Important noteImportant Note:

Silverlight for Windows Phone In Silverlight for Windows Phone the list of headers that can be set is slightly different. For more information see Networking in Silverlight for Windows Phone.

Silverlight 2 and later have support for cross-domain connectivity which allows an application to access resources from locations other than the site of origin. This is an important feature for enabling Silverlight applications to consume existing services on the web. The security policy system in the Silverlight runtime 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 more information, see Network Security Access Restrictions in Silverlight.

For security reasons, the Silverlight runtime restricts access to certain classes of URLs from the WebClient and HTTP classes in the System.Net namespace. There are similar access restrictions applied by the Silverlight runtime to other classes including the Image and MediaElement classes in the System.Windows.Controls namespace. The Silverlight runtime also applies access restrictions 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 example, threats based on a Silverlight application run from an internet server getting access to resources on a local intranet server). For more information, see Network Security Access Restrictions in Silverlight and URL Access Restrictions in Silverlight.

For security reasons, the Silverlight runtime restricts the WebClient class 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.

Version Notes

Silverlight for Windows Phone Silverlight for 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.

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

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

Community Additions

ADD
Show:
© 2014 Microsoft