将动态页面转换成静态页、、、
参数:
savePath:生成静态页的保存路径
saveName:生成静态页的命名
sourceUrl:转换成静态页的动态页Url
请朋友注意一下引用、、、
代码如下:
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.IO;
using System.Text;
using System.Net;
/// <summary>
/// SysImports 的摘要说明
/// 静态页面生成类
/// </summary>
public class SysImports
{
private Encoding code = Encoding.GetEncoding("utf-8");
/*************************************
* 生成静态页
*************************************/
public void createHtml(string savePath, string saveName, string sourceUrl)
{
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//判断路径是否存在
if (Directory.Exists(HttpContext.Current.Server.MapPath(savePath)) == false)
{
//创建路径
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(savePath));
}
WebRequest temp = WebRequest.Create(sourceUrl);
WebResponse mytemp = temp.GetResponse();
sr = new StreamReader(mytemp.GetResponseStream(), _code);
try
{
sr = new StreamReader(mytemp.GetResponseStream(), _code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
try
{
sw = new StreamWriter(HttpContext.Current.Server.MapPath(savePath) + saveName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
}
}
}
工作总是可以积累许经验、、、