Show / Hide Table of Contents

Class WebClient

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

Inheritance
System.Object
WebClient
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
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 Source

WebClient()

Initializes a new instance of the System.Net.WebClient class.

Declaration
public WebClient()

Properties

| Improve this Doc View Source

AllowReadStreamBuffering

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.

| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

IsBusy

Declaration
[NotImplemented]
public bool IsBusy { get; }
Property Value
Type Description
System.Boolean
| Improve this Doc View Source

ResponseHeaders

Declaration
public WebHeaderCollection ResponseHeaders { get; }
Property Value
Type Description
System.Net.WebHeaderCollection

Methods

| Improve this Doc View Source

CancelAsync()

Declaration
[NotImplemented]
public void CancelAsync()
| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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>.

| Improve this Doc View Source

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>.

| Improve this Doc View Source

OpenReadAsync(Uri)

Declaration
[NotImplemented]
public void OpenReadAsync(Uri address)
Parameters
Type Name Description
System.Uri address
| Improve this Doc View Source

OpenReadAsync(Uri, Object)

Declaration
[NotImplemented]
public void OpenReadAsync(Uri address, object userToken)
Parameters
Type Name Description
System.Uri address
System.Object userToken
| Improve this Doc View Source

OpenWriteAsync(Uri)

Declaration
[NotImplemented]
public void OpenWriteAsync(Uri address)
Parameters
Type Name Description
System.Uri address
| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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 Source

DownloadStringCompleted

Occurs when an asynchronous resource-download operation completes.

Declaration
public event DownloadStringCompletedEventHandler DownloadStringCompleted
Event Type
Type Description
DownloadStringCompletedEventHandler
| Improve this Doc View Source

OpenReadCompleted

Declaration
[NotImplemented]
public event OpenReadCompletedEventHandler OpenReadCompleted
Event Type
Type Description
System.Net.OpenReadCompletedEventHandler
| Improve this Doc View Source

OpenWriteCompleted

Declaration
[NotImplemented]
public event OpenWriteCompletedEventHandler OpenWriteCompleted
Event Type
Type Description
System.Net.OpenWriteCompletedEventHandler
| Improve this Doc View Source

UploadStringCompleted

Occurs when an asynchronous string-upload operation completes.

Declaration
public event UploadStringCompletedEventHandler UploadStringCompleted
Event Type
Type Description
UploadStringCompletedEventHandler
  • Improve this Doc
  • View Source