首页 > 代码库 > C# 获取网页源代码

C# 获取网页源代码

        /// <summary>
        /// 获取网页源代码
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private string GetWebRequest(string url)
        {
            if (!url.StartsWith("http://"))
                return "";

            try
            {
                Uri uri = new Uri(url);
                WebRequest myReq = WebRequest.Create(uri);
                WebResponse result = myReq.GetResponse();
                Stream receviceStream = result.GetResponseStream();
                StreamReader readerOfStream = new StreamReader(receviceStream, Encoding.GetEncoding("utf-8"));
                string strHTML = readerOfStream.ReadToEnd();
                readerOfStream.Close();
                receviceStream.Close();
                result.Close();
                return strHTML;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

  

C# 获取网页源代码