1. 程式人生 > >獲取網頁內容WebClient

獲取網頁內容WebClient

  		/// <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;
            }
        }