使用C#怎么生成靜態(tài)頁面

本篇文章為大家展示了使用C#怎么生成靜態(tài)頁面,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

10年積累的做網(wǎng)站、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有漾濞免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

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)行業(yè)資訊頻道。

網(wǎng)站題目:使用C#怎么生成靜態(tài)頁面
鏈接URL:http://bm7419.com/article6/gipiog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、、服務(wù)器托管、電子商務(wù)、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站