溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-06-16 14:46:39 來源:億速云 閱讀:143 作者:Leah 欄目:編程語言

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

private ArrayList htmlCreatedList = new ArrayList();
    /// <summary>
    /// 遞歸實現(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>頁面鏈接對應(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>
    ///正則表達式匹配出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;
    }

上述內(nèi)容就是使用C#怎么生成靜態(tài)頁面,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI