码迷,mamicode.com
首页 > 其他好文 > 详细

动态请求页面生成静态页

时间:2015-11-11 11:21:48      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

话说百度也是这样提高浏览速度的;此案例是控件开发的,MVC或一般处理程序开发的小伙伴们,修修改改一样可以用的;

后台:

 1   protected void btnHtml_Click(object sender, EventArgs e)
 2         {
 3             if (Request.QueryString["id"] != null)
 4             {
 5                 string NewId = Request.QueryString["id"];
 6                 CJRZ.Model.Admin.LoanStrategy modeStrategy = bllNews.GetModel(int.Parse(NewId));
 7                 if (modeStrategy != null)
 8                 {
 9                     CreatHtml cHtml = new CreatHtml();
10                     string url = "http://" + HttpContext.Current.Request.Url.Host.ToString() + ":" + HttpContext.Current.Request.Url.Port;
11                     string AUrl = url + "/raiders/raiders_article.aspx?id=" + modeStrategy.NewID;
12                     //html页面文件名
13                     string fna = Server.MapPath("\\") + "raiders\\" + modeStrategy.NewTypeID + "\\";
14                     string FileName = modeStrategy.NewID + ".html";
15                     if (cHtml.CreateList(AUrl, fna, FileName))
16                     {
17                         modeStrategy.HtmlUrl = url + "/raiders/" + modeStrategy.NewTypeID + "/" + FileName;
18                         hlkLook.NavigateUrl = modeStrategy.HtmlUrl;
19                         if (bllNews.Update(modeStrategy))
20                         {
21                             MessageBox.Show(this, "生成文件成功");
22                         }
23                         else
24                         {
25                             MessageBox.Show(this, "更新数据路径失败!");
26                         }
27                     }
28                     else
29                     {
30                         MessageBox.Show(this, "生成文件失败!");
31                     }
32                 }
33                 else
34                 {
35                     MessageBox.Show(this, "获取新闻对象失败!");
36                 }
37 
38             }
39             else
40             {
41                 MessageBox.Show(this, "获取ID号失败!");
42             }
43         }

前台:

1   <asp:Button ID="btnHtml" runat="server" Text="生成静态页" onclick="btnHtml_Click" Visible="false" />
2                         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
3                            <asp:HyperLink ID="hlkLook" runat="server" style=" color:Blue; font-size:14px;" Target="_blank" >查看页面</asp:HyperLink>

调用的类:

 1   public class CreatHtml
 2     {
 3         public bool CreateList(string url, string fna,string fileName)
 4         {
 5             bool ok;
 6             //准备生成
 7             string strHtml;
 8             StreamReader sr = null; //用来读取流
 9             StreamWriter sw = null; //用来写文件
10             Encoding code = Encoding.GetEncoding("utf-8"); //定义编码
11 
12             //构造web请求,发送请求,获取响应
13             WebRequest HttpWebRequest = null;
14             WebResponse HttpWebResponse = null;
15             HttpWebRequest = WebRequest.Create(url);
16             HttpWebResponse = HttpWebRequest.GetResponse();
17 
18             //获得流
19             sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);
20             strHtml = sr.ReadToEnd();
21 
22             //写入文件
23             try
24             {
25 
26                 if (!Directory.Exists(fna)) 
27                 {
28                     Directory.CreateDirectory(fna); 
29                 }
30                 sw = new StreamWriter(fna+"\\"+fileName, false, code);
31                 sw.WriteLine(strHtml);
32                 sw.Flush();
33                 ok = true;
34             }
35             catch (Exception ex)
36             {
37                 ok = false;
38             }
39             finally
40             {
41                 if (sw != null)
42                 {
43                     sw.Close();
44                 }
45             }
46             return ok;
47         }
48     }

 

动态请求页面生成静态页

标签:

原文地址:http://www.cnblogs.com/woloveprogram/p/4955415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!