c# 获取页面源码

/// <summary>
        /// 获取源代码
        /// </summary>
        /// <param name="url"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public string GetPage(string url, string PointStr,Encoding Ec
        {
            HttpWebRequest request = null;
            HttpWebResponse webResponse = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.AllowAutoRedirect = true;
                request.Timeout = 5000;
                request.ReadWriteTimeout = 5000;
                //request.KeepAlive = true;
                request.Method = "GET";
                //request.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Anonymous;
                request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
                webResponse = (HttpWebResponse)request.GetResponse();
                using (System.IO.Stream stream = webResponse.GetResponseStream())
                {
                    using (System.IO.StreamReader reader2 = new StreamReader(stream, Ec))
                    {
                        return reader2.ReadToEnd();
                    }
                }
            }
            catch
            {
                //Main.St.Add(url+"----"+ex.ToString());
                return "";
            }
            finally
            {
                if (request != null)
                {
                    request = null;
                }
                if (webResponse != null)
                {
                    webResponse.Close();
                    webResponse = null;
                }
            }
        }

 

posted @ 2013-01-10 11:58  落冰  阅读(228)  评论(0编辑  收藏  举报