Click or drag to resize
WebClient Class
Provides common methods for sending data to and receiving data from a resource identified by a URI.
Inheritance Hierarchy
SystemObject
  System.NetWebClient

Namespace: System.Net
Assembly: CSharpXamlForHtml5.System.dll (in CSharpXamlForHtml5.System.dll.dll) Version: 1.0.0.0
Syntax
C#
public class WebClient

The WebClient type exposes the following members.

Constructors
  NameDescription
Public methodWebClient
Initializes a new instance of the System.Net.WebClient class.
Top
Methods
  NameDescription
Public methodDownloadString(String)
Downloads the requested resource as a System.String. The resource to download is specified as a System.String containing the URI.
Public methodDownloadString(Uri)
Downloads the requested resource as a System.String. The resource to download is specified as a System.Uri.
Public methodDownloadStringAsync
Downloads the resource specified as a System.Uri. This method does not block the calling thread.
Public methodDownloadStringTaskAsync(String)
Downloads the resource as a String from the URI specified as an asynchronous operation using a task object.
Public methodDownloadStringTaskAsync(Uri)
Downloads the resource as a String from the URI specified as an asynchronous operation using a task object.
Public methodEquals(Object)
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodUploadString(String, String)
Uploads the specified string to the specified resource, using the POST method.
Public methodUploadString(Uri, String)
Uploads the specified string to the specified resource, using the POST method.
Public methodUploadString(String, String, String)
Uploads the specified string to the specified resource, using the specified method.
Public methodUploadString(Uri, String, String)
Uploads the specified string to the specified resource, using the specified method.
Public methodUploadStringAsync(Uri, String)
Uploads the specified string to the specified resource. This method does not block the calling thread.
Public methodUploadStringAsync(Uri, String, String)
Uploads the specified string to the specified resource. This method does not block the calling thread.
Public methodUploadStringTaskAsync(String, String)
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
Public methodUploadStringTaskAsync(Uri, String)
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
Public methodUploadStringTaskAsync(String, String, String)
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
Public methodUploadStringTaskAsync(Uri, String, String)
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
Top
Properties
  NameDescription
Public propertyEncoding
Gets or sets the System.Text.Encoding used to upload and download strings.
Public propertyHeaders
Gets or sets a collection of header name/value pairs associated with the request.
Top
Events
  NameDescription
Public eventDownloadStringCompleted
Occurs when an asynchronous resource-download operation completes.
Public eventUploadStringCompleted
Occurs when an asynchronous string-upload operation completes.
Top
Examples
Here is an example of a use of the WebClient to receive data:
C#
//We create the WebClient with the right encoding and headers:
var webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
webClient.Headers[HttpRequestHeader.Accept] = "application/xml";

//We submit the request to the server and wait for its response:
string response = await webClient.DownloadStringTaskAsync("http://someAddress.com");

//We modify the response so that it can be deserialized (deserialization is not perfect yet):
response = response.Replace(@"xmlns=""http://NameSpaceOfTheDeserialization""", "");
response = "<ToDoItemsWrapper>" + response.Replace("ArrayOfToDoItem", "ToDoItems") + "</ToDoItemsWrapper>"; // Workaround for the fact that "ArrayOf" types cannot be directly deserialized by the XmlSerializer in this Beta version.

//We create the Deserializer:
var deserializer = new XmlSerializer(typeof(ToDoItemsWrapper));
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(response));
var xmlReader = XmlReader.Create(memoryStream);

//We deserialize:
ToDoItemsWrapper items = (ToDoItemsWrapper)deserializer.Deserialize(xmlReader);
Here is what the ToDoItemsWrapper class looks like (with ToDoItem a Serializable class) :
C#
// Workaround for the fact that "ArrayOf" types cannot directly be deserialized by the XmlSerializer in this Beta version:
[DataContract]
public class ToDoItemsWrapper
{
    public List<ToDoItem> ToDoItems { get; set; }
}
Here is another example that shows how you can use the WebClient to send data:
C#
//We parse the data in a string:
string data = string.Format(@"{{""Id"": ""{0}"",""Description"": ""{1}""}}"Guid.NewGuid(), MyTextBox.Text.Replace("\"", "'"));
//We create the WebClient:
var webClient = new WebClient();
We set the encoding and Headers (note: our data is formatted in json so we set the HttpRequestHeader.ContentType header accordingly) 
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
webClient.Encoding = Encoding.UTF8;
string response = await webClient.UploadStringTaskAsync("http://cshtml5-rest-sample.azurewebsites.net/api/Todo/", "POST", data);
See Also