#region 获取网页源码 (返回String)
    /// <summary>
    /// 获取网页源码 (返回String)
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static string GetHtmlCode(string url)
    {
        try
        {
            string htmlCode;
            HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            webRequest.Referer = null;//最好写请求地址的域名
            webRequest.Timeout = 30000;
            webRequest.Method = "GET";
            webRequest.UserAgent = "Mozilla/4.0";
            webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
            HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
            if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
            {
                using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                {
                    using (var zipStream =
                        new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                    {
                        using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.UTF8))
                        {
                            htmlCode = sr.ReadToEnd();
                        }
                    }
                }
            }
            else
            {
                using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.UTF8))
                    {
                        htmlCode = sr.ReadToEnd();
                    }
                }
            }
            return htmlCode;
        }
        catch
        {
            return string.Empty;
        }
    }
    #endregion

 

相关文章:

  • 2021-11-23
  • 2021-12-21
  • 2021-11-10
  • 2022-12-23
  • 2021-08-04
  • 2021-08-11
  • 2021-10-29
  • 2021-05-19
猜你喜欢
  • 2022-12-23
  • 2021-10-02
  • 2022-12-23
  • 2021-12-06
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案