其他分享
首页 > 其他分享> > HttpWebResponse 获取网页乱码

HttpWebResponse 获取网页乱码

作者:互联网

    1.  StreamReader获取字符串使用 Encoding.Default.
    2.  检查HttpWebResponse.ContentEncoding是否包含恶心的 "GZIP"字符,如果是那么要多一步操作
    3. string html = "";
      string url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/11/1101.html";
      HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
      myReq.Timeout = 5000;//5s
      using (HttpWebResponse httpWResp = (HttpWebResponse)myReq.GetResponse())
      {
      Stream myStream = httpWResp.GetResponseStream();
      //如果包含GZIP,需要解压
      if (!string.IsNullOrEmpty(httpWResp.ContentEncoding) && httpWResp.ContentEncoding.ToUpper().IndexOf("GZIP") > -1)
      {
      System.IO.StreamReader sr =
      new System.IO.StreamReader(new GZipStream(myStream, CompressionMode.Decompress), Encoding.Default);
      html = sr.ReadToEnd();
      sr.Close();
      }
      else
      {
      StreamReader sr = new StreamReader(myStream, Encoding.Default);
      html = sr.ReadToEnd();
      sr.Close();
      }
      myStream.Close();
      httpWResp.Close();

标签:httpWResp,网页,sr,HttpWebResponse,乱码,html,Close,StreamReader
来源: https://www.cnblogs.com/cuihongyu3503319/p/15712757.html