下载网页通用类
/// <summary>/// 根據(jù)網(wǎng)頁(yè)地址去下載網(wǎng)頁(yè),包括從網(wǎng)頁(yè)中獲取自己所需要的內(nèi)容/// </summary>public class WebWrapper{public WebWrapper() { }public WebWrapper(string link) { Link = link; }#region Download htmlpublic 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 contentpublic 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 Propertiespublic 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}
轉(zhuǎn)載于:https://www.cnblogs.com/EasyInvoice/p/3808844.html
總結(jié)
- 上一篇: Report_报表中Ref Cursor
- 下一篇: vim 配置svn