C# webclient中文乱码问题解决方法
更新时间:2016年05月23日 11:27:50 作者:秦风
本文介绍使用webclient抓取远程页面出现乱码的处理方法,希望对大家有所帮助。
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");//向一个连接请求资源
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);
Byte[] pageData = wc.DownloadData("http://m.weather.com.cn/data/101110101.html");
string rr = Encoding.GetEncoding("utf-8").GetString(pageData);
总结下来,还是编码的问题,不论哪种方法,设置好编码即可。
相关文章
C#中Request.Cookies 和 Response.Cookies 的区别分析
本文通过实例代码向我们展示了C#中Request.Cookies 和 Response.Cookies 的区别,文章浅显易懂,这里推荐给大家。2014-11-11
Unity性能优化Shader函数ShaderUtil.GetShaderGlobalKeywords用法示例
这篇文章主要为大家介绍了Unity性能优化Shader函数ShaderUtil.GetShaderGlobalKeywords用法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-09-09


最新评论