WebClient.Encoding 屬性


取得或設定用來上傳和下載字串的 EncodingGets or sets the Encoding used to upload and download strings.

 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,用於編碼字串。A Encoding that is used to encode strings. 這個屬性的預設值為 Default 傳回的編碼方式。The default value of this property is the encoding returned by Default.


下列程式碼範例將示範如何設定這個屬性的值。The following code example demonstrates setting the value of this property.

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


UploadStringUploadStringAsync 方法會使用這個屬性,在 Byte 上傳字串之前將指定的字串轉換為數組。The UploadString and UploadStringAsync methods use this property to convert the specified string to a Byte array before uploading the string. 如需詳細資訊,請參閱 GetBytes 方法。For additional information, see the GetBytes method.

使用或方法下載字串時 DownloadString ,會 DownloadStringAsync WebClient 使用 Encoding 這個所傳回的來將下載的陣列轉換 Byte 成字串。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. 如需詳細資訊,請參閱 GetString 方法。For additional information, see the GetString method.