C# webclient中文乱码问题解决方法

 更新时间:2020年6月25日 11:24  点击:2359

webclient在调用DownloadData或者DownloadString的时候请求回来的数据出现乱码问题,解决办法如下:

1、设置webclient的编码格式为目标编码格式

复制代码 代码如下:
WebClient web = new WebClient();//创建webclient对象
web.Encoding = System.Text.Encoding.UTF8;//定义对象语言
string returns = web.DownloadString("_http://www.weather.com.cn/data/sk/101310101.html");//向一个连接请求资源

2、先获取数据,然后转码

复制代码 代码如下:
WebClient wc = new WebClient();
Byte[] pageData = wc.DownloadData("http://m.weather.com.cn/data/101110101.html");
string rr = Encoding.GetEncoding("utf-8").GetString(pageData);

总结下来,还是编码的问题,不论哪种方法,设置好编码即可。

[!--infotagslink--]

相关文章