使用C#怎么生成静态页面
本篇文章为大家展示了使用C#怎么生成静态页面,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
10年积累的做网站、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有漾濞免费网站建设让你可以放心的选择与我们合作。
private ArrayList htmlCreatedList = new ArrayList(); ////// 递归实现页面静态化功能 /// /// 要访问的页面链接地址 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); } } ////// 通过HttpWebRequest页面链接的html代码 /// /// 页面链接地址 ///页面链接对应的html代码 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; } //////正则表达式匹配出html代码中的超链接 /// ///要找出超链接的html代码 ///private IEnumerable GetUrlLinkFromHtmlCode(string htmlCode) { string strRegex = "href\\s*=\\s*(?:[\"'](?<1>[^\"'.#:]*)[\"'])"; Regex r = new Regex(strRegex, RegexOptions.IgnoreCase); MatchCollection ms = r.Matches(htmlCode); IEnumerable 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#怎么生成静态页面,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
网站题目:使用C#怎么生成静态页面
分享链接:http://myzitong.com/article/gipiog.html