通过网页地址获取网站HTML内容

/// <summary>
        /// 获取网站HTML内容
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetHtmlContent(string address)
        {
            string resultStr = string.Empty;
            HttpWebRequest hreq = null;
            HttpWebResponse hrep = null;
            Stream stream = null;
            StreamReader sReader = null;
            try
            {
                hreq = (HttpWebRequest)WebRequest.Create(address);
                hreq.Timeout = 10000000;
                hrep = (HttpWebResponse)hreq.GetResponse();
                stream = hrep.GetResponseStream();
                sReader = new StreamReader(stream, System.Text.Encoding.Default);
                resultStr = sReader.ReadToEnd();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            finally
            {
                if (sReader != null)
                    sReader.Close();
                if (stream != null)
                    stream.Close();
                if (hrep != null)
                    hrep.Close();
            }
            return resultStr;
        }
posted @ 2010-11-01 13:03  黑色草莓  阅读(699)  评论(0编辑  收藏  举报