陋室铭
永远也不要停下学习的脚步(大道至简至易)

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

posted on 2021-12-20 20:20  宏宇  阅读(166)  评论(0编辑  收藏  举报