您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。