ASP.NET生成静态文件的一个静态类
2009-08-27 21:16
441 查看
某网友刚刚传给我的,稍微看了一下,感觉还不错吧,先记下来,备用,今天早上有段时候JAVAEYE上去好慢。不知道是不是服务器被攻击了。。呵呵。
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();
}
}
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();
}
}
相关文章推荐
- ASP.NET 2.0中,生成一个静态文件的方法
- ASP网站数据采集程序制作:一个采集入库生成本地文件的几个FUCTION(可用来生成HTML静态网页)
- Asp.net生成htm静态文件的两种途径
- ASP.NET生成HTML静态文件的两种途径
- Asp.net生成htm静态文件的两种途径
- ASP网站数据采集程序制作:一个采集入库生成本地文件的几个FUCTION(可用来生成HTML静态网页)
- Asp.net生成htm静态文件的两种途径
- Asp.net生成htm静态文件的两种途径
- Asp.net生成htm静态文件的源代码 2种方法
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- ASP网站数据采集程序制作:一个采集入库生成本地文件的几个FUCTION(可用来生成HTML静态网页)
- Asp.net生成htm静态文件的两种途径
- ASP.NET获取远程网页下载到本地文件,通过URL生成静态文件的做法
- Asp.net生成htm静态文件的两种途径
- Asp.net生成htm静态文件的两种途径
- asp.net生成静态文件
- Asp.net生成htm静态文件的两种途径
- 解析如何利用一个ASP.NET Core应用来发布静态文件
- 在ASP.NET 2.0中,有时候需要对ASP.NET生成的HTML代码进行处理,或者是保存成静态文件。ASP.NET 提供了直接将请求保存成文件的方法:HttpRequest.SaveAs方法。下面这个方法就是在ASP.NET 2.0中得到ASP.NET
- Asp.net生成htm静态文件的两种途径