本篇文章為大家展示了使用C#怎么生成靜態(tài)頁面,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊(cè)、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、麻章網(wǎng)站維護(hù)、網(wǎng)站推廣。private ArrayList htmlCreatedList = new ArrayList(); /// <summary> /// 遞歸實(shí)現(xiàn)頁面靜態(tài)化功能 /// </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>頁面鏈接對(duì)應(yīng)的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> ///正則表達(dá)式匹配出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 擴(kuò)展了一個(gè)方法。
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; }
上述內(nèi)容就是使用C#怎么生成靜態(tài)頁面,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁標(biāo)題:使用C#怎么生成靜態(tài)頁面-創(chuàng)新互聯(lián)
URL分享:http://www.rwnh.cn/article10/cseigo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、軟件開發(fā)、關(guān)鍵詞優(yōu)化、虛擬主機(jī)、網(wǎng)站設(shè)計(jì)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容