using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; using System.Windows.Forms; using System.IO; using System.Threading; using System.Text.RegularExpressions; namespace Yyc.Net { /// <summary> /// 获取网站异步源码类 /// </summary> public class PageSnatch { #region field private WebBrowser browser; /// <summary> /// 默认异步加载延时5s /// </summary> private int timeout; private const int defaultTimeout = 5 * 1000; #endregion #region ctor public PageSnatch() { this.IsBusy = false; //标志为完成状态,可开始新一导航 this.timeout = defaultTimeout; } public PageSnatch(string url) : this() { this.Url = url; //绑定URL } public PageSnatch(string url, int timeout) : this(url) //调用一个构造参数 { this.timeout = timeout; //绑定延时 } #endregion #region event /// <summary> /// 在控件导航到新文档并开始加载该文档时发生 /// </summary> public event SnatchingEventHandler Snatching; /// <summary> /// 在控件完成加载文档时发生 /// </summary> public event SnatchedEventHandler Snatched; /// <summary> /// 异步文档加载完毕发生 /// </summary> public event SnatchCompletedEventHandler SnatchCompleted; #endregion #region property /// <summary> /// 获取或设置是否取消异步数据加载 /// </summary> public bool Cancel { set; get; } /// <summary> /// 获取加载文档是否完成 /// </summary> public bool IsBusy { private set; get; } /// <summary> /// 验证网址是否有效 /// </summary> /// <returns></returns> private bool IsValidate { get { return Regex.IsMatch(Url, @"http(s)?://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?"); } } private string OuterHtml { get { string text = string.Empty; this.Execute(delegate() { text = browser.Document.Body.OuterHtml; }); return text; } } /// <summary> /// 获取或设置加载异步数延时间隔(默认延时,最小设置值5s)