.Net生成HTML的三种方法

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

1
2
3
4
5
6
7
8
9
10
11
12
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();
}
}
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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());
     }
}

  

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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();
         }
     }
}

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。