zoukankan      html  css  js  c++  java
  • 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();
                                }
                            }
                        }
                    }

  • 相关阅读:
    Kotlin调用lambda表达式时,lambda中的参数名字省略问题
    Kotlin读取控制台输入
    安卓P(9)及以上Cleartext HTTP traffic to xxx not permitted错误,无法HTTP明文连接错误解决方法
    AS4.0以上查看R.id
    Kotlin Standard.kt解析(also,apply,let.run...)
    ext4文件解包打包
    Kotlin中的var、val和const
    Kotlin的构造方法探究
    Markdown语法
    uniapp遇到的小问题
  • 原文地址:https://www.cnblogs.com/wangjinming/p/3835366.html
Copyright © 2011-2022 走看看