Class WebClient
Provides common methods for sending data to and receiving data from a resource identified by a URI.
Inheritance
Inherited Members
Namespace: OpenSilver.Compatibility
Assembly: OpenSilver.dll
Syntax
public class WebClient
Examples
Here is an example of a use of the WebClient to receive data:
//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) :
// Workaround for the fact that "ArrayOf" types cannot directly be deserialized by the XmlSerializer in this Beta version:
[DataContract]
public partial class ToDoItemsWrapper
{
public List<ToDoItem> ToDoItems { get; set; }
}
Here is another example that shows how you can use the WebClient to send data:
//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);
Constructors
| Improve this Doc View SourceWebClient()
Initializes a new instance of the System.Net.WebClient class.
Declaration
public WebClient()
Properties
| Improve this Doc View SourceAllowReadStreamBuffering
Gets or sets a value that indicates whether to buffer the data read from the Internet resource for a WebClient instance.
Declaration
[NotImplemented]
public bool AllowReadStreamBuffering { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean | true to enable buffering of the data received from the Internet resource; false to disable buffering. The default is true. |
Encoding
Gets or sets the System.Text.Encoding used to upload and download strings.
Declaration
public Encoding Encoding { get; set; }
Property Value
Type | Description |
---|---|
System.Text.Encoding |
Headers
Gets or sets a collection of header name/value pairs associated with the request.
Declaration
public WebHeaderCollection Headers { get; set; }
Property Value
Type | Description |
---|---|
System.Net.WebHeaderCollection |
IsBusy
Declaration
[NotImplemented]
public bool IsBusy { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
ResponseHeaders
Declaration
public WebHeaderCollection ResponseHeaders { get; }
Property Value
Type | Description |
---|---|
System.Net.WebHeaderCollection |
Methods
| Improve this Doc View SourceCancelAsync()
Declaration
[NotImplemented]
public void CancelAsync()
DownloadString(String)
Downloads the requested resource as a System.String. The resource to download is specified as a System.String containing the URI.
Declaration
public string DownloadString(string address)
Parameters
Type | Name | Description |
---|---|---|
System.String | address | A System.String containing the URI to download. |
Returns
Type | Description |
---|---|
System.String | A System.String containing the requested resource. |
DownloadString(Uri)
Downloads the requested resource as a System.String. The resource to download is specified as a System.Uri.
Declaration
public string DownloadString(Uri address)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address | A System.Uri object containing the URI to download. |
Returns
Type | Description |
---|---|
System.String | A System.String containing the requested resource. |
DownloadStringAsync(Uri)
Downloads the resource specified as a System.Uri. This method does not block the calling thread.
Declaration
public void DownloadStringAsync(Uri address)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address | A System.Uri containing the URI to download. |
DownloadStringTaskAsync(String)
Downloads the resource as a String from the URI specified as an asynchronous operation using a task object.
Declaration
public Task<string> DownloadStringTaskAsync(string address)
Parameters
Type | Name | Description |
---|---|---|
System.String | address | A System.Uri containing the URI to download. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | Returns the resource as System.Threading.Tasks.Task<TResult>. |
DownloadStringTaskAsync(Uri)
Downloads the resource as a String from the URI specified as an asynchronous operation using a task object.
Declaration
public Task<string> DownloadStringTaskAsync(Uri address)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address | A System.Uri containing the URI to download. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | Returns the resource as System.Threading.Tasks.Task<TResult>. |
OpenReadAsync(Uri)
Declaration
[NotImplemented]
public void OpenReadAsync(Uri address)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address |
OpenReadAsync(Uri, Object)
Declaration
[NotImplemented]
public void OpenReadAsync(Uri address, object userToken)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address | |
System.Object | userToken |
OpenWriteAsync(Uri)
Declaration
[NotImplemented]
public void OpenWriteAsync(Uri address)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address |
UploadString(String, String)
Uploads the specified string to the specified resource, using the POST method.
Declaration
public string UploadString(string address, string data)
Parameters
Type | Name | Description |
---|---|---|
System.String | address | The URI of the resource to receive the string. For Http resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page. |
System.String | data | The string to be uploaded. |
Returns
Type | Description |
---|---|
System.String | A System.String containing the response sent by the server. |
UploadString(String, String, String)
Uploads the specified string to the specified resource, using the specified method.
Declaration
public string UploadString(string address, string method, string data)
Parameters
Type | Name | Description |
---|---|---|
System.String | address | The URI of the resource to receive the file. This URI must identify a resource that can accept a request sent with the method method. |
System.String | method | The HTTP method used to send the string to the resource. If null, the default is POST for http and STOR for ftp. |
System.String | data | The string to be uploaded. |
Returns
Type | Description |
---|---|
System.String | A System.String containing the response sent by the server. |
UploadString(Uri, String)
Uploads the specified string to the specified resource, using the POST method.
Declaration
public string UploadString(Uri address, string data)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address | The URI of the resource to receive the string. For Http resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page. |
System.String | data | The string to be uploaded. |
Returns
Type | Description |
---|---|
System.String | A System.String containing the response sent by the server. |
UploadString(Uri, String, String)
Uploads the specified string to the specified resource, using the specified method.
Declaration
public string UploadString(Uri address, string method, string data)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address | The URI of the resource to receive the file. This URI must identify a resource that can accept a request sent with the method method. |
System.String | method | The HTTP method used to send the string to the resource. If null, the default is POST for http and STOR for ftp. |
System.String | data | The string to be uploaded. |
Returns
Type | Description |
---|---|
System.String | A System.String containing the response sent by the server. |
UploadStringAsync(Uri, String)
Uploads the specified string to the specified resource. This method does not block the calling thread.
Declaration
public void UploadStringAsync(Uri address, string data)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address | The URI of the resource to receive the file. For HTTP resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page. |
System.String | data | The string to be uploaded. |
UploadStringAsync(Uri, String, String)
Uploads the specified string to the specified resource. This method does not block the calling thread.
Declaration
public void UploadStringAsync(Uri address, string method, string data)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address | The URI of the resource to receive the file. For HTTP resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page. |
System.String | method | The HTTP method used to send the file to the resource. If null, the default is POST for http and STOR for ftp. |
System.String | data | The string to be uploaded. |
UploadStringTaskAsync(String, String)
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
Declaration
public Task<string> UploadStringTaskAsync(string address, string data)
Parameters
Type | Name | Description |
---|---|---|
System.String | address | The URI of the resource to receive the string. For HTTP resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page. |
System.String | data | The string to be uploaded. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | Returns System.Threading.Tasks.Task<TResult>. The task object representing the asynchronous operation. The Result property on the task object returns a String containing the response sent by the server. |
UploadStringTaskAsync(String, String, String)
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
Declaration
public Task<string> UploadStringTaskAsync(string address, string method, string data)
Parameters
Type | Name | Description |
---|---|---|
System.String | address | The URI of the resource to receive the string. For HTTP resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page. |
System.String | method | The HTTP method used to send the file to the resource. If null, the default is POST for http and STOR for ftp. |
System.String | data | The string to be uploaded. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | Returns System.Threading.Tasks.Task<TResult>. The task object representing the asynchronous operation. The Result property on the task object returns a String containing the response sent by the server. |
UploadStringTaskAsync(Uri, String)
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
Declaration
public Task<string> UploadStringTaskAsync(Uri address, string data)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address | The URI of the resource to receive the string. For HTTP resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page. |
System.String | data | The string to be uploaded. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | Returns System.Threading.Tasks.Task<TResult>. The task object representing the asynchronous operation. The Result property on the task object returns a String containing the response sent by the server. |
UploadStringTaskAsync(Uri, String, String)
Uploads the specified string to the specified resource as an asynchronous operation using a task object.
Declaration
public Task<string> UploadStringTaskAsync(Uri address, string method, string data)
Parameters
Type | Name | Description |
---|---|---|
System.Uri | address | The URI of the resource to receive the string. For HTTP resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page. |
System.String | method | The HTTP method used to send the file to the resource. If null, the default is POST for http and STOR for ftp. |
System.String | data | The string to be uploaded. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> | Returns System.Threading.Tasks.Task<TResult>. The task object representing the asynchronous operation. The Result property on the task object returns a String containing the response sent by the server. |
Events
| Improve this Doc View SourceDownloadStringCompleted
Occurs when an asynchronous resource-download operation completes.
Declaration
public event DownloadStringCompletedEventHandler DownloadStringCompleted
Event Type
Type | Description |
---|---|
DownloadStringCompletedEventHandler |
OpenReadCompleted
Declaration
[NotImplemented]
public event OpenReadCompletedEventHandler OpenReadCompleted
Event Type
Type | Description |
---|---|
System.Net.OpenReadCompletedEventHandler |
OpenWriteCompleted
Declaration
[NotImplemented]
public event OpenWriteCompletedEventHandler OpenWriteCompleted
Event Type
Type | Description |
---|---|
System.Net.OpenWriteCompletedEventHandler |
UploadStringCompleted
Occurs when an asynchronous string-upload operation completes.
Declaration
public event UploadStringCompletedEventHandler UploadStringCompleted
Event Type
Type | Description |
---|---|
UploadStringCompletedEventHandler |