ASP.NET生成HTML静态页的一个类

2009 - 08 - 27
    某网友刚刚传给我的,稍微看了一下,好像不错吧,先记下来,备用!
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

/// <summary>
///CreateHtml 的摘要说明
/// </summary>
public class CreateHtml:System.Web.UI.Page
{
	public CreateHtml()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    /// <summary>
    /// 生成静态页面,生成位置是本项目下
    /// </summary>
    /// <param name="strURL">请求的URL</param>
    /// <param name="strRelPath">创建的路径及文件名,路径为相对路径</param>
    public bool Nei_Create(string strURL, string strRelPath)
    {
        string  strFilePage;
      
        strFilePage = HttpContext.Current.Server.MapPath(strRelPath);
        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.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;
    }
    /// <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(path);
        if (file.Exists)
            file.Delete();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值