WebClient Class |
Namespace: System.Net
public class WebClient
The WebClient type exposes the following members.
Name | Description | |
---|---|---|
![]() | DownloadString(String) |
Downloads the requested resource as a System.String. The resource to download
is specified as a System.String containing the URI.
|
![]() | DownloadString(Uri) |
Downloads the requested resource as a System.String. The resource to download
is specified as a System.Uri.
|
![]() | DownloadStringAsync |
Downloads the resource specified as a System.Uri. This method does not block
the calling thread.
|
![]() | DownloadStringTaskAsync(String) |
Downloads the resource as a String from the URI specified as an asynchronous operation using a task object.
|
![]() | DownloadStringTaskAsync(Uri) |
Downloads the resource as a String from the URI specified as an asynchronous operation using a task object.
|
![]() | Equals(Object) | (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.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
![]() | UploadString(String, String) |
Uploads the specified string to the specified resource, using the POST method.
|
![]() | UploadString(Uri, String) |
Uploads the specified string to the specified resource, using the POST method.
|
![]() | UploadString(String, String, String) |
Uploads the specified string to the specified resource, using the specified
method.
|
![]() | UploadString(Uri, String, String) |
Uploads the specified string to the specified resource, using the specified
method.
|
![]() | 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.
|
![]() | UploadStringTaskAsync(String, String) |
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
|
![]() | UploadStringTaskAsync(Uri, String) |
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
|
![]() | UploadStringTaskAsync(String, String, String) |
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
|
![]() | UploadStringTaskAsync(Uri, String, String) |
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
|
Name | Description | |
---|---|---|
![]() | Encoding |
Gets or sets the System.Text.Encoding used to upload and download strings.
|
![]() | Headers |
Gets or sets a collection of header name/value pairs associated with the
request.
|
Name | Description | |
---|---|---|
![]() | DownloadStringCompleted |
Occurs when an asynchronous resource-download operation completes.
|
![]() | UploadStringCompleted |
Occurs when an asynchronous string-upload operation completes.
|
//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);
// 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; } }
//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);