zoukankan      html  css  js  c++  java
  • 解决下载经过GZip压缩后的网页乱码问题

    目前很多网站默认采用GZip压缩,如果不进行解压缩,下载后生成的html页面打开后会出现中文乱码

    乱码前:

                string url = "http://quote.eastmoney.com/stocklist.html";
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(url);
                    var response = client.GetAsync(url).Result;
    
                    var content = response.Content.ReadAsStringAsync().Result;
                    File.WriteAllText(@"C:stock.html", content, Encoding.Default);
    
                }

    乱码效果:

    解决代码:

                string url = "http://quote.eastmoney.com/stocklist.html";
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(url);
    
                    //关键代码1:设置请求头采用GZip和deflate两种压缩算法
                    client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
                    var response = client.GetAsync(url).Result;
    
                    var fileStream = response.Content.ReadAsStreamAsync().Result;
    
                    //关键代码2:对文件流采用GZip算法解压
                    GZipStream gzip = new GZipStream(fileStream, CompressionMode.Decompress);
    
                    using (StreamReader reader = new StreamReader(gzip, Encoding.GetEncoding("gb2312")))//中文编码处理
                    {
                        File.WriteAllText(@"C:stock.html", reader.ReadToEnd(), Encoding.Default);
                    }
                }

     解决后效果:

    乱码有的时候不能单单靠转File.WriteAllText(@"C:stock.html", reader.ReadToEnd(), Encoding.GetEncoding("gb2312"));方式解决,具体情况具体分析,思维多发散发散。

  • 相关阅读:
    自考新教材-p145_5
    自考新教材-p144_4
    自考新教材-p144_3
    自考新教材-p143_2
    自考新教材-p142_3(1)
    【SQL server】安装和配置
    【,net】发布网站问题
    【LR】关于宽带与比特之间的关系
    【LR】录制测试脚本中的基本菜单
    【LR】安装LR11后遇到的问题
  • 原文地址:https://www.cnblogs.com/wgx0428/p/10261714.html
Copyright © 2011-2022 走看看