经过验证 下面大致5个方法均可以成功转化,但是转化过程中一定要注意原ASPX文件的编码类型,如果是GB2312的 那么不管是在streamReader的时候,还是先将原数据转化成byte流,再转化出HTML字符串的时候一定要注意同原ASPX页面编码类型相同,否则将出现乱码。 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(); } } }
我们开发的asp.net系统中,有些动态的页面常被频繁,如我们的首页index.aspx它涉及到大量的数据库查询工作,当不断有用户它时,服务器便不断向数据库的查询,实际上做了许多重复的工作 服务器端的myPage.aspx 客户端显示myPage.htm 客户端 针对这种资源的浪费情况,我们现在来设计一个解决方案。我们先将那些一段时间内内容不会有什么改变,但又遭大量的动态页面生成静态的页面存放在服务器上,当客户端发出请求时,就让他们直接我们生成的静态页面,过程如下图。 客户端显示myPage.htm 客户端 Execute 服务器端的myPage.aspx 服务器端的myPage.htm 现在我们需要一个后台程序来完成这些事情。 我们可将此做成一个类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 在此处添加构造函数逻辑 // } /// 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内容读到了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();//关闭StringWriter sw.Close();//关闭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内容读到了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();//关闭StringWriter sw.Close();//关闭StreamWriter return true; } catch { return false; } } } }
转自http://www.cnblogs.com/chengyan/archive/2010/12/21/1912335.html 其他 方法: ------------------------------
using system.net; First:在服务器上指定aspx网页,生成html静态页 public partial class Default2 : System.Web.UI.Page Second:在服务器上执行aspx网页时在page_render事件里将本页面生成html静态页 public partial class Default3 : System.Web.UI.Page Third:从指定连接获取源代码生成html静态页 public partial class Default4 : System.Web.UI.Page |
|
来自: 悟静 > 《.net和asp.net》