訪問頁面直接生成靜態頁面,簡單代碼

1..      System.Net.WebClient wc = new System.Net.WebClient();
    wc.DownloadFile(url,path);

上面方法很簡單,之間訪問這個URL地址,就會產生一個HTML頁面,之後直接訪問此HTML頁面,會快很多

還有的比如 httprequest類

 

2.原理同上

/// <summary>
        /// 轉換成靜態HTML
        /// </summary>
        /// <param name="path">動態頁面路徑 如Aritcle.aspx?id=1 </param>
        /// <param name="outpath">生成的HTML文件路徑 </param>     
        public void transHtml(string path, string outpath)
        {
            Page page = new Page();
            StringWriter writer = new StringWriter();
            page.Server.Execute(path, writer);
            FileStream fs;
            if (File.Exists(page.Server.MapPath("") + "//" + outpath))
            {
                File.Delete(page.Server.MapPath("") + "//" + outpath);
                fs = File.Create(page.Server.MapPath("") + "//" + outpath);
            }
            else
            {
                fs = File.Create(page.Server.MapPath("") + "//" + outpath);
            }
            byte[] bt = Encoding.Default.GetBytes(writer.ToString());
            fs.Write(bt, 0, bt.Length);
            fs.Close();
        }

 

不過我感覺還是第一種比較簡單,第二種還是輸入一個URL,進行訪問,得到的內容保存爲HTML文件。

發佈了87 篇原創文章 · 獲贊 6 · 訪問量 64萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章