生成静态页面

来源:互联网 发布:淘宝联盟什么时候提现 编辑:程序博客网 时间:2024/06/07 06:15


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Net;

public partial class Admin_Admin_CreatStaticPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string path = Server.MapPath("~/index.html");
            string url = "http://localhost:3265/WebSite/Default.aspx";
            try {
                StreamWriter swr = new StreamWriter(path, false, Encoding.GetEncoding("utf-8"));

                swr.Write(GetPage(url, Encoding.GetEncoding("utf-8")));
                swr.Close();
                swr.Dispose();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('生成成功')</script>");

            }
            catch
            {

            }
        }
    }

    public string GetPage(string url, Encoding encoding)
    {

        string str = "";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);




        using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
        {
            using (StreamReader sr = new StreamReader(res.GetResponseStream(), encoding))
            {

                str = sr.ReadToEnd();
                sr.Close();

            }

        }



        req.Abort();

        return str;

    }
}
 

原创粉丝点击