WebRequest 获取网页乱码

问题:在用WebRequest获取网页源码时得到的源码是乱码。

原因:1,编码不对

解决办法:设置对应编码

WebRequest request = WebRequest.Create(Url);
WebResponse response = await request.GetResponseAsync();

Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(coding));//这里的coding是页面的编码,可以用Ie右键查看编码。
Result = reader.ReadToEnd();

reader.Dispose();
reader.Dispose();

2,页面进行压缩了

看看html的head,ContentEncoding是否是gzip如果是的话需要解压。//下面的代码是在winrt下的

      WebRequest request = WebRequest.Create(Url);
                WebResponse response = await request.GetResponseAsync();
                Debug.WriteLine(((HttpWebResponse)response).StatusDescription);
                if (response.Headers.AllKeys.Contains("Content-Encoding") && response.Headers["Content-Encoding"].ToLower() == "gzip")//如果使用了GZip则先解压
                {
                    using (System.IO.Stream streamReceive = response.GetResponseStream())
                    {
                        using (var zipStream =
                            new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                        {
                            using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.GetEncoding(coding)))
                            {
                                Result = sr.ReadToEnd();
                            }
                        }
                    }
                }

WebRequest 获取网页乱码,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。