WebClient.Encoding Property

Definition

Gets or sets the Encoding used to upload and download strings.

public System.Text.Encoding Encoding { get; set; }

Property Value

A Encoding that is used to encode strings. The default value of this property is the encoding returned by Default.

Examples

The following code example demonstrates setting the value of this property.

public static void UploadString(string address)
{
    string data = "Time = 12:00am temperature = 50";
    WebClient client = new WebClient();
    // Optionally specify an encoding for uploading and downloading strings.
    client.Encoding = System.Text.Encoding.UTF8;
    // Upload the data.
    string reply = client.UploadString(address, data);
    // Display the server's response.
    Console.WriteLine(reply);
}

Remarks

Forsiktig

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete, and you shouldn't use them for new development. Use HttpClient instead.

The UploadString and UploadStringAsync methods use this property to convert the specified string to a Byte array before uploading the string. For additional information, see the GetBytes method.

When a string is downloaded using the DownloadString or DownloadStringAsync methods, WebClient uses the Encoding returned by this to convert the downloaded Byte array into a string. For additional information, see the GetString method.

Applies to

Produkt Versjoner
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1