c# 获取html页面的html代码

/// <summary>
        /// 获取html页面的html代码 
        /// </summary>
        /// <param name="Url">源网址</param>
        /// <returns>返回html代码</returns>
        public string GetHttpData(string Url)
        {
            string sException = null;
            string sRslt = null;
            WebResponse oWebRps = null;
            WebRequest oWebRqst = WebRequest.Create(Url);
            oWebRqst.Timeout = 50000;
            try
            {
                oWebRps = oWebRqst.GetResponse();
            }
            catch (WebException e)
            {
                sException = e.Message.ToString();
                MessageBox.Show(sException);
            }
            catch (Exception e)
            {
                sException = e.ToString();
                MessageBox.Show(sException);
            }
            finally
            {
                if (oWebRps != null)
                {
                    StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("GB2312"));
                    sRslt = oStreamRd.ReadToEnd();
                    oStreamRd.Close();
                    oWebRps.Close();
                }
            }
            return sRslt;
        }

发表评论

电子邮件地址不会被公开。 必填项已用*标注