WebClient.Encoding 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定用來上傳和下載字串的 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
備註
謹慎
WebRequest
、HttpWebRequest
、ServicePoint
和 WebClient
已經過時,您不應該將它們用於新的開發。 請改用 HttpClient。
UploadString 和 UploadStringAsync 方法會使用這個屬性,在上傳字串之前,先將指定的字串轉換成 Byte 陣列。 如需詳細資訊,請參閱 GetBytes 方法。
使用 DownloadString 或 DownloadStringAsync 方法來下載字串時,WebClient 會使用這個 所傳回的 Encoding,將下載的 Byte 陣列轉換成字元串。 如需詳細資訊,請參閱 GetString 方法。