用HttpWebRequest取得网页源码

作者:互联网   出处:控件中国网   2014-11-05 19:05:53   阅读:1

用HttpWebRequest取得网页源码

/// <summary>  
        /// 用HttpWebRequest取得网页源码  
        /// 对于带BOM的网页很有效,不管是什么编码都能正确识别  
        /// </summary>  
        /// <param name="url">网页地址" </param>   
        /// <returns>返回网页源文件</returns>  
        public static string GetHtmlSource(this string url)
        {
            //处理内容  
            string html = "";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Accept = "*/*"; //接受任意文件
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; // 模拟使用IE在浏览
            request.AllowAutoRedirect = true;//是否允许302
            //request.CookieContainer = new CookieContainer();//cookie容器,
            request.Referer = url; //当前页面的引用

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream, Encoding.Default);
            html = reader.ReadToEnd();
            stream.Close();

            return html;
        }

 

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat