通过URL获得HTML内容

2014-11-09 23:53:51  访问(1819) 赞(0) 踩(0)


        /// <summary>
        /// 通过URL获得HTML内容
        /// </summary>
        /// <param name="strUrl">URL地址</param>
        /// <param name="bKeepAlive"></param>
        /// <param name="timeout"></param>
        /// <param name="encoding">读取HTML的编码模式</param>
        /// <returns></returns>
        public static string GetHttpHtmlData
            (
                string strUrl,
                bool bKeepAlive,
                int timeout,
                Encoding encoding
            )
        {
            if (strUrl == null || strUrl.Length == 0)
                throw new Exception("string strUrl 为空。");

            if (encoding == null)
                encoding = Encoding.Default;

            string theResult = null;
            HttpWebRequest myReq = null;
            HttpWebResponse HttpWResp = null;
            Stream myStream = null;
            StreamReader sr = null;

            try
            {
                myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);


                myReq.KeepAlive = bKeepAlive;


                if (timeout != 0)
                {
                    myReq.Timeout = timeout;
                }

                HttpWResp
                    =
                    (HttpWebResponse)myReq.GetResponse();

                myStream = HttpWResp.GetResponseStream();

                sr = new StreamReader(myStream, encoding);

                StringBuilder strBuilder = new StringBuilder();

                while (-1 != sr.Peek())
                {
                    strBuilder.AppendLine(sr.ReadLine());
                }

                theResult = strBuilder.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                    sr = null;
                }

                if (myStream != null)
                {
                    myStream.Close();
                    myStream.Dispose();
                    myStream = null;
                }

                if (HttpWResp != null)
                {
                    HttpWResp.Close();
                    HttpWResp = null;
                }

                if (myReq != null)
                {
                    myReq = null;
                }

            }

            return theResult;

        }

 

调用例子:


        string strHTML = HtmlSlowXFunctions.GetHttpHtmlData
            (
                "http://www.slowx.net/default.aspx",
                true,
                5000,
                System.Text.Encoding.UTF8
            );


标签:通过URL获得HTML内容    http post 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)