主要思路还是使用StreamReader获取远程url的数据流,然后写.html文件来静态输出。
主要类common.cs封装如下:
Code
1public class common
2{
3 public common()
4 {
5 }
6 /**//// <summary>
7 /// 接收数据流并生成html
8 /// </summary>
9 /// <param name="url">url或者数据流</param>
10 /// <param name="isAlreadyData">是否是数据流</param>
11 /// <param name="fna">生成文件</param>
12 /// <returns>是否成功</returns>
13 public bool CreateList(string url,bool isAlreadyData, string fna)
14 {
15 bool ok;
16 StreamWriter sw = null; //用来写文件
17 Encoding code = Encoding.GetEncoding("utf-8"); //定义编码
18
19 string strHtml;
20 if (isAlreadyData == true)
21 strHtml = url;
22 else
23 strHtml=CommonXMLGetData(url);
24
25 //写入文件
26 try
27 {
28 sw = new StreamWriter(fna, false, code);
29 sw.Write(strHtml);
30 sw.Flush();
31 ok = true;
32 }
33 catch (Exception ex)
34 {
35 HttpContext.Current.Response.Write("<p>写入文件出错:" + ex.Message);
36 HttpContext.Current.Response.End();
37 ok = false;
38 }
39 finally
40 {
41 sw.Close();
42 }
43 return ok;
44 }
45
46 public string GetDataFromRemoteUrl(string url)
47 {
48 return CommonXMLGetData(url);
49 }
50 protected static string CommonXMLGetData(string url)
51 {
52 //准备生成
53 string strHtml;
54 Encoding code = Encoding.GetEncoding("utf-8"); //定义编码
55 StreamReader sr = null; //用来读取流
56 //构造web请求,发送请求,获取响应
57 WebRequest HttpWebRequest = null;
58 WebResponse HttpWebResponse = null;
59 HttpWebRequest = WebRequest.Create(url);
60 try
61 {
62 HttpWebResponse = HttpWebRequest.GetResponse();
63 }
64 catch(Exception ex)
65 {
66 HttpContext.Current.Response.Write("出错信息:" + ex.Message);
67 }
68
69 //获得流
70 sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);
71 strHtml = sr.ReadToEnd();
72 return strHtml;
73 }
74}
1public class common
2{
3 public common()
4 {
5 }
6 /**//// <summary>
7 /// 接收数据流并生成html
8 /// </summary>
9 /// <param name="url">url或者数据流</param>
10 /// <param name="isAlreadyData">是否是数据流</param>
11 /// <param name="fna">生成文件</param>
12 /// <returns>是否成功</returns>
13 public bool CreateList(string url,bool isAlreadyData, string fna)
14 {
15 bool ok;
16 StreamWriter sw = null; //用来写文件
17 Encoding code = Encoding.GetEncoding("utf-8"); //定义编码
18
19 string strHtml;
20 if (isAlreadyData == true)
21 strHtml = url;
22 else
23 strHtml=CommonXMLGetData(url);
24
25 //写入文件
26 try
27 {
28 sw = new StreamWriter(fna, false, code);
29 sw.Write(strHtml);
30 sw.Flush();
31 ok = true;
32 }
33 catch (Exception ex)
34 {
35 HttpContext.Current.Response.Write("<p>写入文件出错:" + ex.Message);
36 HttpContext.Current.Response.End();
37 ok = false;
38 }
39 finally
40 {
41 sw.Close();
42 }
43 return ok;
44 }
45
46 public string GetDataFromRemoteUrl(string url)
47 {
48 return CommonXMLGetData(url);
49 }
50 protected static string CommonXMLGetData(string url)
51 {
52 //准备生成
53 string strHtml;
54 Encoding code = Encoding.GetEncoding("utf-8"); //定义编码
55 StreamReader sr = null; //用来读取流
56 //构造web请求,发送请求,获取响应
57 WebRequest HttpWebRequest = null;
58 WebResponse HttpWebResponse = null;
59 HttpWebRequest = WebRequest.Create(url);
60 try
61 {
62 HttpWebResponse = HttpWebRequest.GetResponse();
63 }
64 catch(Exception ex)
65 {
66 HttpContext.Current.Response.Write("出错信息:" + ex.Message);
67 }
68
69 //获得流
70 sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);
71 strHtml = sr.ReadToEnd();
72 return strHtml;
73 }
74}
以下为页面文件news_list.aspx(前一段时间写的,那时还没有用到存储过程分页)
Code
1 <div id="article">
2 <div id="left">
3 <div id="navPosition">·<a href="index.aspx">首页</a>»·<asp:HyperLink ID="lnkNewsClassName" runat="server"></asp:HyperLink></div>
4 <div id="articleList">
5 <ul>
6 <asp:Repeater ID="rptNewsList" runat="server">
7 <ItemTemplate>
8 <li><asp:HyperLink ID="lnkNewsText" ForeColor="#003399" Text='<%# DataBinder.Eval(Container.DataItem,"News_Id")%>' NavigateUrl='<%# "news_info.aspx?news_id=" + DataBinder.Eval(Container.DataItem,"News_Id")%>' ToolTip='<%# "文章标题:" + DataBinder.Eval(Container.DataItem,"News_Title") + "\n发布时间:" + DataBinder.Eval(Container.DataItem,"News_PostTime") + "\n发布人:" + DataBinder.Eval(Container.DataItem,"News_PostAuthor") %>' Target="_blank" runat="server"></asp:HyperLink><asp:Label ID="lblArticleInfo" ForeColor="red" runat="server"></asp:Label><asp:Label ID="lblArticlePostTime" runat="server" CssClass="postTime"></asp:Label></li>
9 </ItemTemplate>
10 <FooterTemplate>
11 </FooterTemplate>
12 </asp:Repeater>
13 </ul>
14 <div id="articlePager">
15 <asp:HyperLink ID="lnkFirst" NavigateUrl="$firstPage$" runat="server">首页</asp:HyperLink>
16 <asp:HyperLink ID="lnkPre" NavigateUrl="$prePage$" runat="server">上一页</asp:HyperLink>
17 <asp:HyperLink ID="lnkNext" NavigateUrl="$nextPage$" runat="server">下一页</asp:HyperLink>
18 <asp:HyperLink ID="lnkLast" NavigateUrl="$lastPage$" runat="server">尾页</asp:HyperLink>
19 <asp:LinkButton ID="lnkFirstPage" Text="首页" OnCommand="ChangePage" CommandName="FirstPage" runat="server"></asp:LinkButton>
20 <asp:LinkButton ID="lnkPrePage" Text="上一页" OnCommand="ChangePage" CommandName="PrePage" runat="server"></asp:LinkButton>
21 <asp:LinkButton ID="lnkNextPage" Text="下一页" OnCommand="ChangePage" CommandName="NextPage" runat="server"></asp:LinkButton>
22 <asp:LinkButton ID="lnkLastPage" Text="尾页" OnCommand="ChangePage" CommandName="LastPage" runat="server"></asp:LinkButton>
23 <asp:Label CssClass="pager" ID="lblPageInfo" runat="server"></asp:Label>
24 <asp:DropDownList ID="ddlPageUrl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPageUrl_SelectedIndexChanged1"></asp:DropDownList>
25 </div>
26 </div>
27 </div>
28
1 <div id="article">
2 <div id="left">
3 <div id="navPosition">·<a href="index.aspx">首页</a>»·<asp:HyperLink ID="lnkNewsClassName" runat="server"></asp:HyperLink></div>
4 <div id="articleList">
5 <ul>
6 <asp:Repeater ID="rptNewsList" runat="server">
7 <ItemTemplate>
8 <li><asp:HyperLink ID="lnkNewsText" ForeColor="#003399" Text='<%# DataBinder.Eval(Container.DataItem,"News_Id")%>' NavigateUrl='<%# "news_info.aspx?news_id=" + DataBinder.Eval(Container.DataItem,"News_Id")%>' ToolTip='<%# "文章标题:" + DataBinder.Eval(Container.DataItem,"News_Title") + "\n发布时间:" + DataBinder.Eval(Container.DataItem,"News_PostTime") + "\n发布人:" + DataBinder.Eval(Container.DataItem,"News_PostAuthor") %>' Target="_blank" runat="server"></asp:HyperLink><asp:Label ID="lblArticleInfo" ForeColor="red" runat="server"></asp:Label><asp:Label ID="lblArticlePostTime" runat="server" CssClass="postTime"></asp:Label></li>
9 </ItemTemplate>
10 <FooterTemplate>
11 </FooterTemplate>
12 </asp:Repeater>
13 </ul>
14 <div id="articlePager">
15 <asp:HyperLink ID="lnkFirst" NavigateUrl="$firstPage$" runat="server">首页</asp:HyperLink>
16 <asp:HyperLink ID="lnkPre" NavigateUrl="$prePage$" runat="server">上一页</asp:HyperLink>
17 <asp:HyperLink ID="lnkNext" NavigateUrl="$nextPage$" runat="server">下一页</asp:HyperLink>
18 <asp:HyperLink ID="lnkLast" NavigateUrl="$lastPage$" runat="server">尾页</asp:HyperLink>
19 <asp:LinkButton ID="lnkFirstPage" Text="首页" OnCommand="ChangePage" CommandName="FirstPage" runat="server"></asp:LinkButton>
20 <asp:LinkButton ID="lnkPrePage" Text="上一页" OnCommand="ChangePage" CommandName="PrePage" runat="server"></asp:LinkButton>
21 <asp:LinkButton ID="lnkNextPage" Text="下一页" OnCommand="ChangePage" CommandName="NextPage" runat="server"></asp:LinkButton>
22 <asp:LinkButton ID="lnkLastPage" Text="尾页" OnCommand="ChangePage" CommandName="LastPage" runat="server"></asp:LinkButton>
23 <asp:Label CssClass="pager" ID="lblPageInfo" runat="server"></asp:Label>
24 <asp:DropDownList ID="ddlPageUrl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPageUrl_SelectedIndexChanged1"></asp:DropDownList>
25 </div>
26 </div>
27 </div>
28
其中带$...$标签的为页码标记。
以下是生成代码,生成内容包括这里的news_list.aspx和news_info.aspx文件,也就是所有关于news的文件。
Code
//全部生成html
protected void MakeHtml(object sender, EventArgs e)
{
btnMakeHtml.Enabled = false;
btnMakeHtml.Text = "生成可能需要一段时间,请耐心等待……";
common cmn = new common();
string url = "http://" + Request.ServerVariables["Http_Host"] + Request.ServerVariables["URL"];
url = url.Substring(0, url.LastIndexOf("/"));
url = url.Substring(0, url.LastIndexOf("/"));
url = url + "/template/";
string fp = Server.MapPath("manage_news.aspx");
fp = fp.Substring(0, fp.LastIndexOf("\\"));
fp = fp.Substring(0, fp.LastIndexOf("\\"));
fp = fp + "\\html\\news\\";
//生成news_list.aspx
if (!Directory.Exists(fp))
{
Directory.CreateDirectory(fp);
}
else
{
string urlList;//获取数据网址
string fpHtml;//生成文件名(含路径)
// int iPageSize = Hope_T_NewManager.GetHope_T_NewsNum();
IList<Hope_T_NewsClass> listNewsClass = Hope_T_NewsClassManager.GetAllHope_T_NewsClassesIndex();
foreach (Hope_T_NewsClass list in listNewsClass)
{
int iNewsNumByClass = Hope_T_NewManager.GetHope_T_NewsNumByNews_ClassId(list.News_ClassId);
if (iNewsNumByClass > 30)//大于一
{
int iPageSize = iNewsNumByClass / 30;
for (int i = 1; i <= iPageSize + 1; i++)
{
urlList = url + "news_list.aspx?news_classid=" + list.News_ClassId + "&page=" + i.ToString();
if (i == 1)
fpHtml = fp + "news_list" + list.News_ClassId + ".html";
else
fpHtml = fp + "news_list" + list.News_ClassId + "_" + i.ToString() + ".html";
string sContent = cmn.GetDataFromRemoteUrl(urlList);
sContent = sContent.Replace("$firstPage$", "news_list" + list.News_ClassId + "_1.html");//首页
sContent = sContent.Replace("$prePage$", "news_list" + list.News_ClassId + "_" + (i - 1).ToString() + ".html");//上一页
sContent = sContent.Replace("$nextPage$", "news_list" + list.News_ClassId + "_" + (i + 1).ToString() + ".html");//下一页
sContent = sContent.Replace("$lastPage$", "news_list" + list.News_ClassId + "_" + (iPageSize + 1).ToString() + ".html");//尾页
IList<Hope_T_Ad> listAd = Hope_T_AdManager.GetAllAd();
foreach (Hope_T_Ad list1 in listAd)
{
sContent = sContent.Replace(list1.Ad_Label, list1.Ad_Code);
}
cmn.CreateList(sContent, true, fpHtml);
Hope_T_Log objLog = new Hope_T_Log();
objLog.Log_Content = "生成文章类别" + list.News_ClassName.ToString();
objLog.Log_Time = Convert.ToDateTime(System.DateTime.Now.ToString());
objLog.Log_User = Session["HopeUser"].ToString();
Hope_T_LogManager.AddHope_T_Log(objLog);
}
}
// else
// {
urlList = url + "news_list.aspx?news_classid=" + list.News_ClassId.ToString();
fpHtml = fp + "news_list" + list.News_ClassId + ".html";
string sContentList = cmn.GetDataFromRemoteUrl(urlList);
IList<Hope_T_Ad> listAdList = Hope_T_AdManager.GetAllAd();
foreach (Hope_T_Ad list1 in listAdList)
{
sContentList = sContentList.Replace(list1.Ad_Label, list1.Ad_Code);
}
sContentList = sContentList.Replace("$firstPage$", "news_list" + list.News_ClassId + ".html");//首页
sContentList = sContentList.Replace("$prePage$", "news_list" + list.News_ClassId + ".html");//上一页
sContentList = sContentList.Replace("$nextPage$", "news_list" + list.News_ClassId + ".html");//下一页
sContentList = sContentList.Replace("$lastPage$", "news_list" + list.News_ClassId + ".html");//尾页
cmn.CreateList(sContentList, true, fpHtml);
// }
}
}
//生成全部news_info.aspx
IList<Hope_T_New> listNews = Hope_T_NewManager.GetAllHope_T_News();
StringBuilder sb = new StringBuilder();
foreach (Hope_T_New list in listNews)
{
string strUrl = url + "news_info.aspx?news_id=" + list.News_Id.ToString();
string strFilePath = fp + Convert.ToDateTime(list.News_PostTime.ToString()).ToShortDateString().Replace("-", "") + list.News_Id.ToString() + ".html";
string sContent = cmn.GetDataFromRemoteUrl(strUrl);
IList<Hope_T_Ad> listAd = Hope_T_AdManager.GetAllAd();
foreach (Hope_T_Ad list1 in listAd)
{
sContent = sContent.Replace(list1.Ad_Label, list1.Ad_Code);
}
if (cmn.CreateList(sContent, true, strFilePath))
{
Hope_T_Log objLog = new Hope_T_Log();
objLog.Log_Content = "生成全部文章";
objLog.Log_Time = Convert.ToDateTime(System.DateTime.Now.ToString());
objLog.Log_User = Session["HopeUser"].ToString();
Hope_T_LogManager.AddHope_T_Log(objLog);
Response.Write("<script>alert('已全部生成成功');location.href=location.href;</script>");
}
}
btnMakeHtml.Text = "重新生成";
btnMakeHtml.Enabled = true;
}
本人是新手,技术很菜,恳请高手不吝赐教。
//全部生成html
protected void MakeHtml(object sender, EventArgs e)
{
btnMakeHtml.Enabled = false;
btnMakeHtml.Text = "生成可能需要一段时间,请耐心等待……";
common cmn = new common();
string url = "http://" + Request.ServerVariables["Http_Host"] + Request.ServerVariables["URL"];
url = url.Substring(0, url.LastIndexOf("/"));
url = url.Substring(0, url.LastIndexOf("/"));
url = url + "/template/";
string fp = Server.MapPath("manage_news.aspx");
fp = fp.Substring(0, fp.LastIndexOf("\\"));
fp = fp.Substring(0, fp.LastIndexOf("\\"));
fp = fp + "\\html\\news\\";
//生成news_list.aspx
if (!Directory.Exists(fp))
{
Directory.CreateDirectory(fp);
}
else
{
string urlList;//获取数据网址
string fpHtml;//生成文件名(含路径)
// int iPageSize = Hope_T_NewManager.GetHope_T_NewsNum();
IList<Hope_T_NewsClass> listNewsClass = Hope_T_NewsClassManager.GetAllHope_T_NewsClassesIndex();
foreach (Hope_T_NewsClass list in listNewsClass)
{
int iNewsNumByClass = Hope_T_NewManager.GetHope_T_NewsNumByNews_ClassId(list.News_ClassId);
if (iNewsNumByClass > 30)//大于一
{
int iPageSize = iNewsNumByClass / 30;
for (int i = 1; i <= iPageSize + 1; i++)
{
urlList = url + "news_list.aspx?news_classid=" + list.News_ClassId + "&page=" + i.ToString();
if (i == 1)
fpHtml = fp + "news_list" + list.News_ClassId + ".html";
else
fpHtml = fp + "news_list" + list.News_ClassId + "_" + i.ToString() + ".html";
string sContent = cmn.GetDataFromRemoteUrl(urlList);
sContent = sContent.Replace("$firstPage$", "news_list" + list.News_ClassId + "_1.html");//首页
sContent = sContent.Replace("$prePage$", "news_list" + list.News_ClassId + "_" + (i - 1).ToString() + ".html");//上一页
sContent = sContent.Replace("$nextPage$", "news_list" + list.News_ClassId + "_" + (i + 1).ToString() + ".html");//下一页
sContent = sContent.Replace("$lastPage$", "news_list" + list.News_ClassId + "_" + (iPageSize + 1).ToString() + ".html");//尾页
IList<Hope_T_Ad> listAd = Hope_T_AdManager.GetAllAd();
foreach (Hope_T_Ad list1 in listAd)
{
sContent = sContent.Replace(list1.Ad_Label, list1.Ad_Code);
}
cmn.CreateList(sContent, true, fpHtml);
Hope_T_Log objLog = new Hope_T_Log();
objLog.Log_Content = "生成文章类别" + list.News_ClassName.ToString();
objLog.Log_Time = Convert.ToDateTime(System.DateTime.Now.ToString());
objLog.Log_User = Session["HopeUser"].ToString();
Hope_T_LogManager.AddHope_T_Log(objLog);
}
}
// else
// {
urlList = url + "news_list.aspx?news_classid=" + list.News_ClassId.ToString();
fpHtml = fp + "news_list" + list.News_ClassId + ".html";
string sContentList = cmn.GetDataFromRemoteUrl(urlList);
IList<Hope_T_Ad> listAdList = Hope_T_AdManager.GetAllAd();
foreach (Hope_T_Ad list1 in listAdList)
{
sContentList = sContentList.Replace(list1.Ad_Label, list1.Ad_Code);
}
sContentList = sContentList.Replace("$firstPage$", "news_list" + list.News_ClassId + ".html");//首页
sContentList = sContentList.Replace("$prePage$", "news_list" + list.News_ClassId + ".html");//上一页
sContentList = sContentList.Replace("$nextPage$", "news_list" + list.News_ClassId + ".html");//下一页
sContentList = sContentList.Replace("$lastPage$", "news_list" + list.News_ClassId + ".html");//尾页
cmn.CreateList(sContentList, true, fpHtml);
// }
}
}
//生成全部news_info.aspx
IList<Hope_T_New> listNews = Hope_T_NewManager.GetAllHope_T_News();
StringBuilder sb = new StringBuilder();
foreach (Hope_T_New list in listNews)
{
string strUrl = url + "news_info.aspx?news_id=" + list.News_Id.ToString();
string strFilePath = fp + Convert.ToDateTime(list.News_PostTime.ToString()).ToShortDateString().Replace("-", "") + list.News_Id.ToString() + ".html";
string sContent = cmn.GetDataFromRemoteUrl(strUrl);
IList<Hope_T_Ad> listAd = Hope_T_AdManager.GetAllAd();
foreach (Hope_T_Ad list1 in listAd)
{
sContent = sContent.Replace(list1.Ad_Label, list1.Ad_Code);
}
if (cmn.CreateList(sContent, true, strFilePath))
{
Hope_T_Log objLog = new Hope_T_Log();
objLog.Log_Content = "生成全部文章";
objLog.Log_Time = Convert.ToDateTime(System.DateTime.Now.ToString());
objLog.Log_User = Session["HopeUser"].ToString();
Hope_T_LogManager.AddHope_T_Log(objLog);
Response.Write("<script>alert('已全部生成成功');location.href=location.href;</script>");
}
}
btnMakeHtml.Text = "重新生成";
btnMakeHtml.Enabled = true;
}