今天在用WebClient调用DownloadString方法获取数据时发现有中文乱码的问题,网上找到两个解决方法:
1.设置WebClient对象的编码格式
public static string Get(string url) { var wc = new WebClient(); wc.Encoding = System.Text.Encoding.UTF8;//解决获取用户信息乱码问题 return wc.DownloadString(url); }
2.获得数据之后再进行编码
var wc = new WebClient(); Byte[] getData = wc.DownloadData(url); string rr = Encoding.GetEncoding("UTF-8").GetString(getData );