1 #region 生成静态页 2 /// <summary> 3 /// 生成静态页 4 /// </summary> 5 /// <param name="URL">要生成的页面</param> 6 /// <param name="savePath">保存位置</param> 7 /// <param name="htmlName">页面名称</param> 8 /// <returns></returns> 9 public string Buiding(string URL, string savePath, string htmlName) 10 { 11 try 12 { 13 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);//向url所在的服务器发送一个请求 14 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //获得服务器的回应 15 string charset = response.CharacterSet; 16 Encoding encode = Encoding.GetEncoding(charset); 17 TextReader text = new StreamReader(response.GetResponseStream(), encode);//初始化流 18 string htmlContent = text.ReadToEnd();//获得网页内容 19 response.Close(); 20 string path = savePath + htmlName + ".html"; 21 FileInfo t = new FileInfo(Server.MapPath(path));//将网页内容保存到html文件 22 StreamWriter html = t.CreateText(); 23 html.WriteLine(htmlContent); 24 html.Close(); 25 return path; 26 } 27 catch (Exception) 28 { 29 throw; 30 } 31 } 32 33 /// <summary> 34 /// 根据模版生成静态页 35 /// </summary> 36 /// <returns></returns> 37 public string BuidingByModel() 38 { 39 try 40 { 41 Encoding encode = Encoding.GetEncoding("utf-8");//设置编码方式 42 TextReader tr = new StreamReader(Server.MapPath("~/Model.html"), encode);//读取模版页初始化流 43 string htmlContent = tr.ReadToEnd();//获得网页内容 44 htmlContent = htmlContent.Replace("$NowTime$", "替换文本"); 45 tr.Close(); 46 string path = "~/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".html"; 47 FileInfo t = new FileInfo(Server.MapPath(path));//将网页内容保存到html文件 48 StreamWriter html = t.CreateText(); 49 html.WriteLine(htmlContent); 50 html.Close(); 51 return path; 52 } 53 catch (Exception) 54 { 55 56 throw; 57 } 58 } 59 #endregion 60 61 生成静态页