欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取网页内容WebClient

程序员文章站 2024-02-21 22:29:16
...
  		/// <summary>
        /// 获取网页内容
        /// </summary>
        /// <param name="url">网址</param>
        private static string GetHtmlString(string url)
        {
            try
            {
                int timeOut = 0;
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Byte[] pageData = null;
                WebClient MyWebClient;
                int timeOutCount = 0;
                while (true)
                {
                    if (sw.ElapsedMilliseconds > timeOut)
                    {
                        MyWebClient = new WebClient();
                        MyWebClient.DownloadDataCompleted += (o, e) =>
                        {
                            sw.Reset();
                            if (pageData == null)
                            {
                                pageData = e.Result;
                            }
                        };
                        MyWebClient.DownloadDataAsync(new Uri(url));
                        sw.Restart();
                        timeOut = 5000;
                        timeOutCount ++;
                    }
                    Thread.Sleep(1000);
                    if (pageData != null)
                    {
                        break;
                    }
                    if (timeOutCount == 3)
                    {
                        sw.Stop();
                        return null;
                    }
                }
                sw.Stop();
                string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8
                return pageHtml;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }