在一些需要經(jīng)常更新頁(yè)面數(shù)據(jù)的網(wǎng)站中,一般訪(fǎng)問(wèn)量不是很大的都直接發(fā)布的是帶后臺(tái)代碼,每次訪(fǎng)問(wèn)都是有數(shù)據(jù)庫(kù)交互的。但是一旦訪(fǎng)問(wèn)量增加了,那么這些服務(wù)器開(kāi)銷(xiāo)變成本就要考慮進(jìn)來(lái)了,像一些文章,后臺(tái)編輯后,文章內(nèi)容存入數(shù)據(jù)庫(kù),如果1000人訪(fǎng)問(wèn),如果還是每次取數(shù)據(jù)庫(kù),那這1000次的io訪(fǎng)問(wèn)就顯得比較大了,一個(gè)好的方法就是,文章確定之后,做成靜態(tài)頁(yè)面,而這個(gè)做的方法由程序來(lái)做,就是遞歸遍歷整個(gè)網(wǎng)站,將網(wǎng)站內(nèi)容都訪(fǎng)問(wèn)一遍,然后生成這些頁(yè)面的靜態(tài)文本頁(yè)面,在將這些頁(yè)面發(fā)布,這樣對(duì)瀏覽者而言,他看到的還是同一個(gè)地址,同一份文章,只是這份是靜態(tài)的而言。這樣就提升了網(wǎng)站的效率節(jié)約了資源;
下面附上一份C#遍歷網(wǎng)站內(nèi)容,然后生成內(nèi)容頁(yè)面代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
private ArrayList htmlCreatedList = new ArrayList(); /// <summary> /// 遞歸實(shí)現(xiàn)頁(yè)面靜態(tài)化功能 /// </summary> /// <param name="urlString">要訪(fǎng)問(wèn)的頁(yè)面鏈接地址</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> /// 通過(guò)HttpWebRequest頁(yè)面鏈接的html代碼 /// </summary> /// <param name="urlString">頁(yè)面鏈接地址</param> /// <returns>頁(yè)面鏈接對(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è)方法。
1
2
3
4
5
6
7
8
9
10
11
|
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; } |
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/chenqiangdage/article/details/49821189