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;
            }
        }

  

posted @ 2017-05-27 16:22  懒人境界  阅读(669)  评论(0编辑  收藏  举报