返回顶部

收藏

获取页面源代码

更多
/// <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;
                }
            }
        }
//该片段来自于http://outofmemory.cn

标签:c#,.net,网络

收藏

0人收藏

支持

0

反对

0

发表评论