本篇文章为大家展示了使用C#怎么生成静态页面,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
private ArrayList htmlCreatedList = new ArrayList(); /// <summary> /// 递归实现页面静态化功能 /// </summary> /// <param name="urlString">要访问的页面链接地址</param> public void SaveHtmlCode(string urlString) { if (htmlCreatedList.Contains(urlString)) { return; } string htmlCode = GetHtmlCodeFromUrl(urlString); string htmlPath = urlString.ToPhysicalPath(); string direcHtmlPath = Path.GetDirectoryName(htmlPath); if (!Directory.Exists(direcHtmlPath)) { Directory.CreateDirectory(direcHtmlPath); } File.WriteAllText(htmlPath, htmlCode); htmlCreatedList.Add(urlString); var urlList = GetUrlLinkFromHtmlCode(htmlCode); string urlTemp = string.Empty; foreach (string url in urlList) { urlTemp = url; urlTemp = Regex.Replace(urlTemp, "href\\s*=\\s*", ""); urlTemp = urlTemp.Replace("\"", ""); urlTemp = urlTemp.Replace("\\", "/"); urlTemp = WebConfigInfo.UrlPrefix + urlTemp; SaveHtmlCode(urlTemp); } } /// <summary> /// 通过HttpWebRequest页面链接的html代码 /// </summary> /// <param name="urlString">页面链接地址</param> /// <returns>页面链接对应的html代码</returns> private string GetHtmlCodeFromUrl(string urlString) { HttpWebRequest hwRequest = (HttpWebRequest)WebRequest.Create(urlString); hwRequest.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705"; hwRequest.Accept = "*/*"; hwRequest.KeepAlive = true; hwRequest.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5"); HttpWebResponse hwResponse = (HttpWebResponse)hwRequest.GetResponse(); Stream streamResponse = hwResponse.GetResponseStream(); StreamReader readerOfStream = new StreamReader(streamResponse, System.Text.Encoding.GetEncoding("utf-8")); string strHtml = readerOfStream.ReadToEnd(); readerOfStream.Close(); streamResponse.Close(); hwResponse.Close(); return strHtml; } ///<summary> ///正则表达式匹配出html代码中的超链接 ///</summary> ///<param name="htmlCode">要找出超链接的html代码</param> ///<returns></returns> private IEnumerable<string> GetUrlLinkFromHtmlCode(string htmlCode) { string strRegex = "href\\s*=\\s*(?:[\"'](?<1>[^\"'.#:]*)[\"'])"; Regex r = new Regex(strRegex, RegexOptions.IgnoreCase); MatchCollection ms = r.Matches(htmlCode); IEnumerable<string> listUrl = from Match cc in ms select cc.ToString().Replace("&", "&"); return listUrl.Distinct(); } }
给string 扩展了一个方法。
public static string ToPhysicalPath(this string urlString) { System.Uri uri = new System.Uri(urlString); string htmlPath = string.Format("{0}\\Html\\{1}\\", System.Web.HttpContext.Current.Request.PhysicalApplicationPath, uri.AbsolutePath); string[] querys = uri.Query.Split(new char[] { '?', '&', '=' }, StringSplitOptions.RemoveEmptyEntries); htmlPath += string.Join(string.Empty, querys); htmlPath += querys.Length.Equals(0) ? "Index.html" : ".html"; htmlPath = htmlPath.Replace("/", "\\"); htmlPath = htmlPath.Replace("\\\\", "\\"); return htmlPath; }
上述内容就是使用C#怎么生成静态页面,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。