C#中WebClient中文乱码的解决办法

第一次尝试: 

string question = textBox1.Text.ToString();
WebClient youdao = new WebClient();
youdao.Encoding = System.Text.Encoding.GetEncoding("GB2312");
Uri uri = new Uri("http://xxxxxxxxxxxxxx");
textBox1.Text =youdao.DownloadString(uri);

返回的字符串中有中文,出现乱码

尝试n次后,解决办法如下:

string question = textBox1.Text.ToString();
WebClient youdao = new WebClient();
youdao.Encoding = System.Text.Encoding.GetEncoding("GB2312");
Uri uri = new Uri("http://xxxxxxxxxxxxxx");
textBox1.Text =Encoding.UTF8.GetString(youdao.DownloadData(uri));


先获取最基本的byte[]类型返回数据,然后转码成UTF8即可 

你可能感兴趣的:(c#)