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();

上一篇:2021/9/22-java中Scanner对象的关闭使用


下一篇:普利姆算法