• 下载网页通用类


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

      

  • 相关阅读:
    求字符串的全排列
    不能被继承的类
    Apache2启动错误以及Ubuntu update的错误
    从尾到头输出链表
    教你在网页上加QQ链接
    UL LI P 图片加文字无缝滚动
    ASP.net 里怎么对fileUpload控件上传的文件进行管理
    表单标签
    如果我为我女朋友做了这些,她一定会娇滴滴的说:“你真坏!
    break,continue,return
  • 原文地址:https://www.cnblogs.com/EasyInvoice/p/3808844.html
Copyright © 2020-2023  润新知