WebClient.Encoding 属性

定义

获取或设置用于上传和下载字符串的 Encoding

public:
 property System::Text::Encoding ^ Encoding { System::Text::Encoding ^ get(); void set(System::Text::Encoding ^ value); };
public System.Text.Encoding Encoding { get; set; }
member this.Encoding : System.Text.Encoding with get, set
Public Property Encoding As Encoding

属性值

用于对字符串进行编码的 Encoding。 此属性的默认值是 Default返回的编码。

示例

下面的代码示例演示如何设置此属性的值。

void UploadString( String^ address )
{
   String^ data = "Time = 12:00am temperature = 50";
   WebClient^ client = gcnew 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 );

   // Disply the server's response.
   Console::WriteLine( reply );
}
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);
}
Public Shared Sub UploadString(ByVal address As String)

    Dim data As String = "Time = 12:00am temperature = 50"
    Dim client As WebClient = New WebClient()
    '  Optionally specify an encoding for uploading and downloading strings.
    client.Encoding = System.Text.Encoding.UTF8
    '  Upload the data.
    Dim reply As String = client.UploadString(address, data)
    '  Disply the server's response.
    Console.WriteLine(reply)
End Sub

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

UploadStringUploadStringAsync 方法在上传字符串之前使用此属性将指定的字符串转换为 Byte 数组。 有关详细信息,请参阅 GetBytes 方法。

使用 DownloadStringDownloadStringAsync 方法下载字符串时,WebClient 使用由此返回的 Encoding 将下载的 Byte 数组转换为字符串。 有关详细信息,请参阅 GetString 方法。

适用于