码迷,mamicode.com
首页 > Web开发 > 详细

asp.net生成HTML静态页

时间:2014-06-25 19:35:14      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   tar   

using System;
using System.Data;
using System.Configuration;
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.Text.RegularExpressions;
using System.IO;

/// <summary>
/// CreateHtml 的摘要说明
/// asp.net生成HTML静态页
/// 调用方法:
/// new CreateHtml().Nei_Create("http://localhost:4032/new5mdn/default.aspx", "default.htm"); 
/// </summary>
public class CreateHtml :System.Web.UI.Page
{
    public CreateHtml()
    {
        
    }
    /// <summary>
    /// 生成静态页面,生成位置是本项目下
    /// </summary>
    /// <param name="strURL">请求的URL</param>
    /// <param name="strRelPath">创建的路径及文件名,路径为相对路径</param>
    /// <returns></returns>
    public bool Nei_Create(string strURL, string strRelPath)
    {
        string strFilePage;
        strFilePage = HttpContext.Current.Server.MapPath(strFilePage);
        StreamWriter sw = null;

        //获得aspx的静态html
        try
        {
            if (File.Exists(strFilePage))
            {
                File.Delete(strFilePage);
            }
            sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("gb2312"));
            System.Net.WebRequest wReq = System.Net.WebRequest.Create(strURL);
            System.Net.WebResponse wResp = wReq.GetRequestStream();
            System.IO.Stream respStream = wResp.GetResponseStream();
            System.IO.StreamReader reader = new StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
            string strTemp = reader.ReadToEnd();

            Regex r1 = new Regex("<input type=\"hidden\" name=\"__EVENTTARGET\".*/>", RegexOptions.IgnoreCase);
            Regex r2 = new Regex("<input type=\"hidden\" name=\"__EVENTARGUMENT\".*/>", RegexOptions.IgnoreCase);
            Regex r3 = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\".*/>", RegexOptions.IgnoreCase);

            Regex r4 = new Regex("<form .*id=\"form1\">", RegexOptions.IgnoreCase);
            Regex r5 = new Regex("</form>");

            Regex r6 = new Regex("<input type=\"hidden\" name=\"__EVENTVALIDATION\".*/>", RegexOptions.IgnoreCase);
            strTemp = r1.Replace(strTemp, "");
            strTemp = r2.Replace(strTemp, "");
            strTemp = r3.Replace(strTemp, "");
            strTemp = r4.Replace(strTemp, "");
            strTemp = r5.Replace(strTemp, "");
            strTemp = r6.Replace(strTemp, "");

            sw.Write(strTemp);   
        }
        catch(Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
            return false; //生成到出错
        }
        finally
        {
            sw.Flush();
            sw.Close();
            sw = null;
        }
    }

    /// <summary>   
    /// 生成静态页面,生成位置不在本项目下   
    /// </summary>   
    /// <param name="strURL">请求的URL</param>   
    /// <param name="strRelPath">创建的路径及文件名,路径为绝对路径</param>   
    public bool Wai_Create(string strURL, string strRelPath, string filename)
    {
        string strFilePage;
        strFilePage = strRelPath + "\\" + filename;
        StreamWriter sw = null;
        //获得aspx的静态html   
        try
        {
            if (!Directory.Exists(strRelPath))
            {
                Directory.CreateDirectory(strRelPath);
            }
            if (File.Exists(strFilePage))
            {
                File.Delete(strFilePage);
            }
            sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("gb2312"));
            System.Net.WebRequest wReq = System.Net.WebRequest.Create(strURL);
            System.Net.WebResponse wResp = wReq.GetResponse();
            System.IO.Stream respStream = wResp.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
            string strTemp = reader.ReadToEnd();

            Regex r1 = new Regex("<input type=\"hidden\" name=\"__EVENTTARGET\".*/>", RegexOptions.IgnoreCase);
            Regex r2 = new Regex("<input type=\"hidden\" name=\"__EVENTARGUMENT\".*/>", RegexOptions.IgnoreCase);
            Regex r3 = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\".*/>", RegexOptions.IgnoreCase);

            Regex r4 = new Regex("<form .*id=\"form1\">", RegexOptions.IgnoreCase);
            Regex r5 = new Regex("</form>");

            Regex r6 = new Regex("<input type=\"hidden\" name=\"__EVENTVALIDATION\".*/>", RegexOptions.IgnoreCase);
            strTemp = r1.Replace(strTemp, "");
            strTemp = r2.Replace(strTemp, "");
            strTemp = r3.Replace(strTemp, "");
            strTemp = r4.Replace(strTemp, "");
            strTemp = r5.Replace(strTemp, "");
            strTemp = r6.Replace(strTemp, "");

            sw.Write(strTemp);
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
            return false;//生成到出错   
        }
        finally
        {
            sw.Flush();
            sw.Close();
            sw = null;
        }

        return true;

    }   

    public void FilePicDelete(string path)
    {
        System.IO.FileInfo file = new System.IO.FileInfo();
        if (file.Exists)
            file.Delete();
    }
}

asp.net生成HTML静态页,布布扣,bubuko.com

asp.net生成HTML静态页

标签:style   class   blog   code   http   tar   

原文地址:http://www.cnblogs.com/yiyilian/p/3805627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!