小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

ASPX文件轉(zhuǎn)HTML

 悟靜 2012-11-10

 經(jīng)過驗證 下面大致5個方法均可以成功轉(zhuǎn)化,但是轉(zhuǎn)化過程中一定要注意原ASPX文件的編碼類型,如果是GB2312的 那么不管是在streamReader的時候,還是先將原數(shù)據(jù)轉(zhuǎn)化成byte流,再轉(zhuǎn)化出HTML字符串的時候一定要注意同原ASPX頁面編碼類型相同,否則將出現(xiàn)亂碼。

using System.Net;
using System.Text;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    public StreamWriter sw;
    protected void Page_Load(object sender, EventArgs e)
    {
      
        WebClient myWebClient = new WebClient();
        myWebClient.Credentials = CredentialCache.DefaultCredentials;
        byte[] pagedata = myWebClient.DownloadData("http://home./default.aspx");
        string myDataBuffer = Encoding.Default.GetString(pagedata);
        string path = HttpContext.Current.Server.MapPath(".");
        Encoding code = Encoding.GetEncoding("gb2312");
        string htmlfilename = "test.html";
        try
        {
            FileStream fs = new FileStream(path+"/"+htmlfilename, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.WriteLine(myDataBuffer);
            sw.Close();
            fs.Close();
            Response.Write("生成成功了!ok");
        }
        catch (Exception ex)
        {
            File.Delete(path + htmlfilename);
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
            Response.Write("生成失敗了!no");
        }
        finally
        {
            if (sw != null)
                sw.Close();
        }
    }
}

 

我們開發(fā)的asp.net系統(tǒng)中,有些動態(tài)的頁面常被頻繁,如我們的首頁index.aspx它涉及到大量的數(shù)據(jù)庫查詢工作,當(dāng)不斷有用戶它時,服務(wù)器便不斷向數(shù)據(jù)庫的查詢,實(shí)際上做了許多重復(fù)的工作

服務(wù)器端的myPage.aspx

客戶端顯示myPage.htm

客戶端

針對這種資源的浪費(fèi)情況,我們現(xiàn)在來設(shè)計一個解決方案。我們先將那些一段時間內(nèi)內(nèi)容不會有什么改變,但又遭大量的動態(tài)頁面生成靜態(tài)的頁面存放在服務(wù)器上,當(dāng)客戶端發(fā)出請求時,就讓他們直接我們生成的靜態(tài)頁面,過程如下圖。

客戶端顯示myPage.htm

客戶端

Execute

服務(wù)器端的myPage.aspx

服務(wù)器端的myPage.htm

現(xiàn)在我們需要一個后臺程序來完成這些事情。

我們可將此做成一個類classAspxToHtml ,其代碼

using System;

using System.IO;

using System.Web.UI;

namespace LeoLu

{

/// summary

/// AspxToHtml 的摘要說明。

/// /summary

public class AspxToHtml

{

/// summary

/// Aspx文件url

/// /summary

public string strUrl;

/// summary

/// 生成html文件的保存路徑

/// /summary

public string strSavePath;

/// summary

/// 生成html文件的文件名

/// /summary

public string strSaveFile;

public AspxToHtml()

{

//

// TOD 在此處添加構(gòu)造函數(shù)邏輯

//

}

/// summary

/// 將strUrl放到strSavePath目錄下,保存為strSaveFile

/// /summary

/// returns是否成功/returns

public bool ExecAspxToHtml()

{

try

{

StringWriter strHTML = new StringWriter();

System.Web.UI.Page myPage = new Page(); //System.Web.UI.Page中有個Server對象,我們要利用一下它

myPage.Server.Execute(strUrl,strHTML);//將asp_net.aspx將在客戶段顯示的html內(nèi)容讀到了strHTML中

StreamWriter sw = new StreamWriter(strSavePath+strSaveFile,true,System.Text.Encoding.GetEncoding("GB2312"));

//新建一個文件Test.htm,文件格式為GB2312

sw.Write(strHTML.ToString()); //將strHTML中的字符寫到Test.htm中

strHTML.Close();//關(guān)閉StringWriter

sw.Close();//關(guān)閉StreamWriter

return true;

}

catch

{

return false;

}

}

/// summary

/// 將Url放到Path目錄下,保存為FileName

/// /summary

/// param name="Url"aspx頁面url/param

/// param name="Path"生成html文件的保存路徑/param

/// param name="FileName"生成html文件的文件名/param

/// returns/returns

public bool ExecAspxToHtml(string Url,string Path,string FileName)

{

try

{

StringWriter strHTML = new StringWriter();

System.Web.UI.Page myPage = new Page(); //System.Web.UI.Page中有個Server對象,我們要利用一下它

myPage.Server.Execute(Url,strHTML); //將asp_net.aspx將在客戶段顯示的html內(nèi)容讀到了strHTML中

StreamWriter sw = new StreamWriter(Path+FileName,true,System.Text.Encoding.GetEncoding("GB2312"));

//新建一個文件Test.htm,文件格式為GB2312

sw.Write(strHTML.ToString()); //將strHTML中的字符寫到Test.htm中

strHTML.Close();//關(guān)閉StringWriter

sw.Close();//關(guān)閉StreamWriter

return true;

}

catch

{

return false;

}

}

}

}

 

 

 

轉(zhuǎn)自http://www.cnblogs.com/chengyan/archive/2010/12/21/1912335.html

其他 方法:

------------------------------

 

 

using system.net;
using system.io;   

First:在服務(wù)器上指定aspx網(wǎng)頁,生成html靜態(tài)頁

public partial class Default2 : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             StreamWriter sw = new StreamWriter(Server.MapPath("靜態(tài)頁1.htm"), false, System.Text.Encoding.GetEncoding("gb2312"));
             Server.Execute("Default3.aspx", sw);
             sw.Close();
         }
     }
}

Second:在服務(wù)器上執(zhí)行aspx網(wǎng)頁時在page_render事件里將本頁面生成html靜態(tài)頁

public partial class Default3 : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
       
     }
     protected override void Render(HtmlTextWriter writer)
     {
         StringWriter html = new StringWriter();
         System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html);
         base.Render(tw);
         System.IO.StreamWriter sw;
         sw = new System.IO.StreamWriter(Server.MapPath("靜態(tài)頁2.htm"), false, System.Text.Encoding.Default);
         sw.Write(html.ToString());
         sw.Close();
         tw.Close();
         Response.Write(html.ToString());
     }
}

Third:從指定連接獲取源代碼生成html靜態(tài)頁

public partial class Default4 : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             string pageurl = "http://www.baidu.com";
             WebRequest request = WebRequest.Create(pageurl);
             WebResponse response = request.GetResponse();
             Stream resstream = response.GetResponseStream();
             StreamReader sr = new StreamReader(resstream, System.Text.Encoding.Default);
             string contenthtml = sr.ReadToEnd();
             resstream.Close();
             sr.Close(); //寫入文件
             System.IO.StreamWriter sw;
             sw = new System.IO.StreamWriter(Server.MapPath("靜態(tài)頁生成方法3.htm"), false, System.Text.Encoding.Default);
             sw.Write(contenthtml);
             sw.Close();
         }
     }
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多