/// <summary> /// 根据网页地址去下载网页,包括从网页中获取自己所需要的内容 /// </summary> public class WebWrapper { public WebWrapper() { } public WebWrapper(string link) { Link = link; } #region Download html public string Link { get; set; } public string DownLoadHtml(string link) { string htmlString = string.Empty; try { WebRequest webRequest = System.Net.WebRequest.Create(link); webRequest.Method = "GET"; webRequest.ContentType = "application/x-www-form-urlencoded"; using (StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream())) { htmlString = sr.ReadToEnd(); } } catch (Exception ex) { throw new ApplicationException(string.Format("Error Downloading CurrencyRate: {0}", ex.Message)); } return htmlString; } public string DownLoadHtml() { if (string.IsNullOrEmpty(Link)) throw new ApplicationException("Please input web address"); return DownLoadHtml(Link); } #endregion #region Get matched content public string GetContent(string html, string regexExpress) { Regex re = new Regex(regexExpress); MatchCollection ms = re.Matches(html); if (ms == null || ms.Count == 0) throw new ApplicationException("Can not find matched content"); return ms[0].ToString(); } public string GetContent(string regexExpress) { string html = DownLoadHtml(); return GetContent(html, regexExpress); } #endregion #region Regex Properties public string BeginConstruction = "(?<=({0}))"; public string EndConstruction = "(?=({0}))"; public string RegexBeginExpression(string begin) { return string.Format(BeginConstruction, begin); } public string RegexEndExpression(string end) { return string.Format(EndConstruction, end); } public string RegexAnyExpression { get { return "[.\s\S]*?"; } } #endregion }