C#動態生成靜態頁面

.NET 的WebRequest類提供了許多WEB請求功能,方便了Windows Form和Web Form對WEB的交互操作,加上Stream的操作,可以很方便的通過URL將動態頁面 生成靜態頁面,並保存在本地或服務器 上,因爲是通過URL請求,所以不存在語言和編碼的限制,同時,可以適用於Windows Form和Web Form,下面柯男寫了一個簡單的類,來完成一些基本操作。

  1. StaticPage.cs  
  2. using System;  
  3. using System.Data;  
  4. using System.Configuration;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.Html Controls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Net;  
  14. using System.IO;  
  15. /// <summary>  
  16. ///URL動態保存靜態頁面  
  17. /// </summary>  
  18. public class StaticPage  
  19. {  
  20.     private string _url="";  
  21.     private WebRequest request;  
  22.     private WebResponse response;  
  23. public StaticPage()  
  24. {  
  25.    
  26. }  
  27.     public StaticPage(string url)  
  28.     {  
  29.         _url = url;  
  30.     }  
  31.     public string Url  
  32.     {  
  33.         get  
  34.         {  
  35.             return _url;  
  36.         }  
  37.         set  
  38.         {  
  39.             _url = value;  
  40.         }  
  41.     }  
  42.     public bool Save(string FilePath)  
  43.     {  
  44.         bool _isOk = false;  
  45.         if(_url!="")  
  46.         {  
  47.             request = WebRequest.Create(_url);  
  48.             response=request.GetResponse();  
  49.             if(File.Exists(FilePath))  
  50.             {  
  51.                 File.Delete(FilePath);  
  52.             }  
  53.             StreamReader reader = new StreamReader(response.GetResponseStream(),System.Text.Encoding.Default);  
  54.             StreamWriter write = new StreamWriter(FilePath,false, System.Text.Encoding.Default);  
  55.             write.Write(reader.ReadToEnd());  
  56.             write.Flush();  
  57.             write.Close();  
  58.             write = null;  
  59.             _isOk = true;  
  60.         }  
  61.         return _isOk;  
  62.     }  
  63. }  
複製代碼
  1. cai.asp x  
  2. <%@ Page Language="C# " %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <script. runat="server">  
  5.     private void Page_Load(object sender, EventArgs e)  
  6.     {  
  7.          
  8.     }  
  9.     private void saveBtn_click(object sender, EventArgs e)  
  10.     {  
  11.         if (path.Text.ToString() != "")  
  12.         {  
  13.             StaticPage page = new StaticPage();  
  14.             page.Url = path.Text.ToString();  
  15.             string filePath = Server.MapPath("temp.html");  
  16.             if (page.Save(filePath))  
  17.             {  
  18.                 Response.Redirect("temp.html");  
  19.             }  
  20.         }  
  21.     }         
  22. </script>  
  23. <html xmlns="http://www.w3.org/1999/xhtml">  
  24. <head runat="server">  
  25.     <title>動態生成靜態頁面文件</title>  
  26.     <style. type="text/css">  
  27.     input  
  28.     {  
  29.       padding:4px;  
  30.       font-size:12px;  
  31.     }  
  32.     </style>  
  33. </head>  
  34. <body>  
  35.     <form. id="form1" runat="server">  
  36.     <div>  
  37.     <asp:TextBox ID="path" runat="server" Width="300px"></asp:TextBox>  
  38.     <asp:Button ID="saveBtn" runat="server" Text="生成靜態文件/" nClick="saveBtn_click" />  
  39.     </div>  
  40.     </form>  
  41. </body>  
  42. </html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章