zoukankan      html  css  js  c++  java
  • ASPX文件转HTML

     经过验证 下面大致5个方法均可以成功转化,但是转化过程中一定要注意原ASPX文件的编码类型,如果是GB2312的 那么不管是在streamReader的时候,还是先将原数据转化成byte流,再转化出HTML字符串的时候一定要注意同原ASPX页面编码类型相同,否则将出现乱码。

    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.xmhouse.com/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;
    using system.io;   

    First:在服务器上指定aspx网页,生成html静态页

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

    Second:在服务器上执行aspx网页时在page_render事件里将本页面生成html静态页

    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("静态页2.htm"), false, System.Text.Encoding.Default);
             sw.Write(html.ToString());
             sw.Close();
             tw.Close();
             Response.Write(html.ToString());
         }
    }

    Third:从指定连接获取源代码生成html静态页

    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("静态页生成方法3.htm"), false, System.Text.Encoding.Default);
                 sw.Write(contenthtml);
                 sw.Close();
             }
         }
    }

  • 相关阅读:
    0108 创建表约束
    Mybatis 将数据库中查出的记录,一对多返回,即分组,然后返回每个组的所有数据
    SQL主表、从表
    MySQL中添加、删除字段,使用SQL语句操作
    git 将远程工作分支合并到本地dev分支
    MySQL inner join 和 left join 的区别
    Mysql union 和 order by 同时使用需要注意的问题
    The used SELECT statements have a different number of columns
    Every derived table must have its own alias(MySQL报错:每个派生表都必须有自己的别名)
    MySQL 日期格式化及字符串、date、毫秒互相转化
  • 原文地址:https://www.cnblogs.com/kaixinmenghuan/p/2058278.html
Copyright © 2011-2022 走看看