生成静态页
来源:互联网 发布:win10不能安装软件 编辑:程序博客网 时间:2024/05/22 00:50
添加一个类文件makefile:
#region 根据绝对url生成Htm页面
public bool makeUrlhtm(string pageUrl, string filePath, string fileName,string basepath)
{
string filePage = HttpContext.Current.Server.MapPath(filePath + fileName);
StreamWriter sw = null;
//获得静态html
try
{
if (!Directory.Exists(HttpContext.Current.Server.MapPath(filePath)))
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filePath));
}
if (File.Exists(filePage))
{
File.Delete(filePage);
}
sw = new StreamWriter(filePage, false, Encoding.GetEncoding("GB2312"));
WebRequest wReq = WebRequest.Create(pageUrl);
WebResponse wResp = wReq.GetResponse();
Stream respStream = wResp.GetResponseStream();
StreamReader reader = new StreamReader(respStream, Encoding.GetEncoding("gb2312"));
string finalHtml = reader.ReadToEnd();
finalHtml = ClearInput(finalHtml);
//string basepath = "<base href='http://www.cupledu.com/StudyCenter/FsJobgrinds/' /><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
if (finalHtml.IndexOf(@"<head>")>0)
{
finalHtml.Insert(finalHtml.IndexOf(@"<head>")+6,basepath);
}
sw.Write(finalHtml);
//HttpContext.Current.Response.Write("<script language='JavaScript'>alert('生成成功!');</script>");
return true;
}
catch
{
//HttpContext.Current.Response.Write("<script language='JavaScript'>alert('生成失败!');</script>");
return false;//生成到出错
}
finally
{
sw.Flush();
sw.Close();
sw = null;
}
}
#endregion
#region ClearInput
public string ClearInput(string finalHtml)
{
string pattern = @"<input type=""hidden"" name=""__VIEWSTATE""[^>]*>";//清除hidden内容
string pattern1 = @"<input type=""hidden"" name=""__EVENTVALIDATION""[^>]*>";//清除hidden内容
string FormTargetBegin = @"<form name=[^>]*>";//清除Form
string FormTargetEnd = @"</form>";
//取得匹配值
string s = ((System.Text.RegularExpressions.Capture)(Regex.Match(finalHtml, pattern).Groups.SyncRoot)).Value;
finalHtml = Regex.Replace(finalHtml, pattern, string.Empty, RegexOptions.IgnoreCase | RegexOptions.Multiline);
finalHtml = Regex.Replace(finalHtml, pattern1, string.Empty, RegexOptions.IgnoreCase | RegexOptions.Multiline);
finalHtml = Regex.Replace(finalHtml, FormTargetBegin, string.Empty, RegexOptions.IgnoreCase | RegexOptions.Multiline);
finalHtml = Regex.Replace(finalHtml, FormTargetEnd, string.Empty, RegexOptions.IgnoreCase | RegexOptions.Multiline);
return finalHtml;
}
#endregion
调用:
makeFile function = new makeFile();
string url = "要下载的页面路径如www.baidu.com";
string severpath = Server.MapPath("~/");
string basepath = "<base href='http://www.cupledu.com/StudyCenter/FsJobgrinds/' /><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
function.makeUrlhtm(url, searpath, "zfindex.html", basepath);
- 如何生成静态页
- 如何生成静态页
- 如何生成静态页
- 非摸板生成静态页
- aspx生成静态页
- 动态生成静态页
- 如何生成静态页
- 生成静态页
- 非摸板生成静态页
- PHP生成静态页
- smarty生成静态页
- php生成静态页
- PHP生成静态页
- php生成静态页
- 生成 shtml 静态页
- 生成静态页
- aspx生成静态页
- 生成静态页
- Windows 8的意义,不仅仅是Metro
- 《Essential C++》读书笔记(四)
- ListView的用法
- sqlserver存储过程与即时查询的计划缓存
- android sharedpreferences 详解 代码 实例
- 生成静态页
- 【设计模式】之模板方法(Template Method)
- 什么是MVC以及PHP如何实现MVC .
- 限制 Domain User 加域设置
- 浅解用PHP实现MVC .
- Jquery validate 验证
- android遇到的一些错误汇总
- ACE_Reactor 机制下的其他事件触发器
- 几个图论和复杂网络的程序库 —— BGL,QuickGraph,igraph和NetworkX