生成静态页面(转)
using System.Web;
using System.IO;
using System.Net;
/// <summary> /// 根据aspx页面地址生成html页面 /// </summary> /// <param name="AspxPageUrl">aspx页面地址</param> /// <param name="FolderPath">html文件存放文件夹路径</param> /// <param name="HtmlPageName">html页面名称若为空则按时间命名</param> /// <returns></returns> public bool PageToHtml(string AspxPageUrl, string FolderPath, string HtmlPageName) { bool flag = false; Encoding code = Encoding.GetEncoding("utf-8"); StreamReader sr = null; StreamWriter sw = null; string str = null; //读取远程路径 WebRequest temp = WebRequest.Create(AspxPageUrl); 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(); } string fileName = string.Empty; if (!string.IsNullOrEmpty(HtmlPageName)) { fileName = HtmlPageName + ".htm"; } else { fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm"; } //写入 try { sw = new StreamWriter(HttpContext.Current.Server.MapPath(FolderPath + "/") + fileName, false, code); sw.Write(str); sw.Flush(); } catch (Exception ex) { throw ex; } finally { sw.Close(); flag = true; } return flag; }