实名认证用户熊川湘 身份证号码430811198506290914

ASP.NET生成静态方法

CMS系统如果新闻多了,全部生成静态的话。不现实,而且占用空间比较大。那么只生成网站首页是必须的了,下面列出JCMS首页生成静态的方法。换一种思路其实更简单。

当点击生成首页静态的时候。去获取动态首页的源代码,然后在网站根目录生成一个index.htm的文件为静态首页就好了

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
protected void Button2_Click(object sender, EventArgs e)
       {
           string asd = GetHtml(""+new JCMS.BLL.Jcms_sysconfig().GetModelByCache(1).Doname+"/index.aspx", "utf-8");
           FileStream fs= File.Create(Server.MapPath("~/index.htm"));
              
   
           StreamWriter sw = new StreamWriter(fs,Encoding.UTF8);
           sw.Write(asd);
              
           sw.Close();
           fs.Close();
           Response.Write("<script>alert('操作完成!');location.href='" + Request.Url.AbsoluteUri + "'</script>");
   
       }

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/// <summary>
       /// 根据提供的地址和编码下载网页。
       /// </summary>
       /// <param name="url">url是要访问的网站地址</param>
       /// <param name="charSet">charSet是目标网页的编码,如果传入的是null或者"",那就自动分析网页的编码</param>
       /// <returns></returns>
       public static string GetHtml(string url, string charSet)
       {
           string strResult = string.Empty;
           try
           {
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
               //request.Timeout = 0x9c40;
               request.Timeout = 60000;
               request.Headers.Set("Pragma", "no-cache");
               Stream streamReceive = ((HttpWebResponse)request.GetResponse()).GetResponseStream();
               Encoding encoding = Encoding.Default;
               if (!string.IsNullOrEmpty(charSet) && Encoding.GetEncoding(charSet) != Encoding.Default)
                   encoding = Encoding.GetEncoding(charSet);
               strResult = new StreamReader(streamReceive, encoding).ReadToEnd();
           }
           catch (Exception ex)
           {
               //Global.LogInfo = "未能获取服务器数据,请稍候再试!" + ex.Message;
           }
           return strResult;
       }
   
   
       /// <summary>
       /// 
       /// </summary>
       /// <param name="url"></param>
       /// <param name="SavaPath"></param>
       public static void SavaWebFile(string url, string SavaPath)
       {
           //指针的位置
           long lStartPos = 0;
           int READ_SIZE = 8 * 1024;
           FileStream fs;
           if (File.Exists(SavaPath))
           {
               fs = File.OpenWrite(SavaPath);
               lStartPos = fs.Length;
               fs.Seek(lStartPos, SeekOrigin.Current);
           }
           else
           {
               fs = new FileStream(SavaPath, FileMode.OpenOrCreate, FileAccess.Write);
           }
           //打开网络连接
           HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
           HttpWebResponse myHttpWebResponse = null;
           if (lStartPos > 0)
           {
               request.AddRange((int)lStartPos);//设置Range值 
           }
           try
           {
               myHttpWebResponse = (HttpWebResponse)request.GetResponse();
           }
           catch (WebException e)
           {
               if (fs != null)
                   fs.Close();
               if (myHttpWebResponse != null)
                   myHttpWebResponse.Close();
               //Global.LogInfo = "未能获取服务器数据,请稍候再试!" + e.Message;
           }
           //向服务器请求,获得服务器回应数据流 
           Stream strm = myHttpWebResponse.GetResponseStream();
           //开始下载。
           byte[] buffer = new byte[READ_SIZE];
           int bufferSize = buffer.Length;
           int readCount = READ_SIZE;
           while (readCount > 0)
           {
               readCount = strm.Read(buffer, 0, bufferSize);
               if (0 == readCount)
               {
                   break;
               }
               fs.Write(buffer, 0, readCount);
           }
           fs.Close();
           myHttpWebResponse.Close();
           strm.Close();
       }

这样生成后。看看网站打开是不是飞快了?

posted @ 2014-04-17 21:56  浪达短信群发  阅读(166)  评论(0编辑  收藏  举报