共用方式為


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 方法。

適用於