RSS原理和实现
??????? RSS是在互聯網上被廣泛采用的內容包裝和投遞協議。網絡用戶可以在客戶端借助于支持RSS的新聞工具軟件,在不打開網站內容頁面的情況下,閱讀支持RSS輸出的網站內容。
1.RSS文件結構
??????? 示例:
<?xml?version="1.0"?encoding="gb2312"??>?<rss?version="2.0">
<channel>?
<title>我的Blog</title>?????????????????//channel的標題
<description>與我自己的技術Blog相關聯</description>???//channel的介紹
<link>http://counter.csdn.net/pv.aspx?id=72</link>?????//channel的url
<item>?
<title><!--?項標題?--></title>???????????//item的標題
<link><!--?項?URL?--></link>???????????//item的url
<description><!--?簡要描述?--></description>????????//item的介紹
<!--?可選的/可擴展的元素?-->????????//item的其他屬性,比如更新時間
</item>?
<item>
<!--?可多個<item>項目-->???????????//一個channel有多個item
</item>
</channel>
</rss>
??????? RSS是兩級結構,第一級結構是channel,相當于blog系統中某人的blog,第二級結構是item,相當于blog中的文章。屬性中最重要的是title、description和link,title是標題,description是介紹,link是與其相關的url。?
2.RSS的使用
??????? 有的網站提供了RSS自動發現機制,可以很方便地把RSS的URL添加到RSS閱讀器中。如果沒有自動發現,那么可以手動把RSS鏈接的URL添加到RSS閱讀器中,這樣就加入了一個用戶訂閱的頻道。在RSS閱讀器中可以更新頻道列表或點擊一個item鏈接打開該item的頁面。?
3.RSS的工作機制
??????? 如下圖所示:
??????? 內容提供者在其網站上添加RSS的鏈接,以提供RSS訂閱功能,當打開這個鏈接時,傳送過去了一些頻道信息,比如:blog的作者名。
??????? 一種做法是,RSS鏈接URL指向的是一個空內容的頁面,該頁面后臺程序通過傳過來的頻道信息訪問數據庫,獲取頻道列表,用Response.Write向該空頁面寫出XML格式的文件。
??????? 另一種做法是,RSS鏈接URL指向的是一個xml文件,該文件由服務器的程序事先生成好的,放在服務器上,訪問時靜態獲取,服務器在作者每添加一個頻道列表時自動更新該xml文件。
??????? 第一種做法的優點是管理方便,因為不需要為每個頻道生成xml文件,所有的RSS請求都由一個后臺頁面處理,接口統一,但每次訪問RSS鏈接時,都要動態地寫出RSS頻道列表,訪問效率相對較低,第二種做法的優點是訪問時,只是返回一個靜態的xml文件,不需要訪問數據庫來臨時生成,所以訪問效率相對較高,但每更新一次頻道列表中的項時,就要自動地重新生成xml文件以保證RSS文件的最新,這樣就降低了更新的效率。本系統中采用的是第一種方法。?
4.RSS的實現
??????? RSS有兩大部件:RSS鏈接和RSS閱讀器。
4.1RSS鏈接的實現?
4.1.1 RSS頁面
(1)頁面文件上使用標記來確定格式
??????? 用Repeater作為xml的載體,并不真正生成xml文件。<![CDATA[ ]]>語句用于轉義,否則將被識別為有標記的文本塊。?
?RssFeed.aspx??
?
<%@?Page?language="c#"?Codebehind="RssFeed.aspx.cs"?AutoEventWireup="false"?Inherits="MyRss.RssFeed"?%><asp:Repeater?id="Repeater1"?runat="server">
<HeaderTemplate>
<rss?version="2.0">?
<channel>?
<title>NewsShow</title>?
<link>http://192.168.1.7/MainOne_HZ/News/</link>?
<description>Rss?Feed?for?192.168.1.7</description>?
</HeaderTemplate>
?
<ItemTemplate>
<item>?
<title><%#?FormatForXML(DataBinder.Eval(Container.DataItem,"newsname"))?%></title>?
<description><![CDATA[<%#FormatForXML(DataBinder.Eval(Container.DataItem,"newsinfo"))%>]]></description>?
<link>http://192.168.1.7/MainOne_HZ/News/NewsShow.aspx?ID=<%#?DataBinder.Eval(Container.DataItem,?"newsid")?%></link>?
<pubDate><%#?String.Format("{0:R}",DataBinder.Eval(Container.DataItem,"wtime"))?%></pubDate>?
</item>
</ItemTemplate>
?
<FooterTemplate>
</channel>
</rss>
</FooterTemplate>
</asp:Repeater>
RssFeed.aspx.cs
private?void?Page_Load(object?sender,?System.EventArgs?e)??...{
???//?在此處放置用戶代碼以初始化頁面
???if(!this.IsPostBack)
???...{
????Response.ContentType="text/xml";
????SqlConnection?con=new?SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
????SqlCommand?cmd=new?SqlCommand("select?top?20?*?from?news?order?by?newsid?desc",con);
????con.Open();
????SqlDataReader?dr=cmd.ExecuteReader();
????Repeater1.DataSource=dr;
????Repeater1.DataBind();
????dr.Close();
????con.Close();
???}???
??}
??protected?string?FormatForXML(object?input)
??...{
???string?data=input.ToString();
???data=data.Replace("&","&");
???data=data.Replace("/",""");
???data=data.Replace("'","&qapos;");
???data=data.Replace("<","<");
???data=data.Replace(">",">");
???return?data;
??}
(2)后臺cs文件使用Response.Write來輸出格式?
4.1.2寫成xml文件
4.1.3使用RSS工具包?
4.2RSS閱讀器的實現
原理是把寫有rss內容的xml文件讀出來,然后再按結構寫成html的格式
?
<%@?Page?Language="C#"?AutoEventWireup="true"??CodeFile="Default.aspx.cs"?Inherits="_Default"?%><!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html?xmlns="http://www.w3.org/1999/xhtml"?>
<head?id="Head1"?runat="server">
????<title>簡易RSS閱讀器</title>
<script?language="javascript"?type="text/javascript">
?
//?<!CDATA[
function?ButtonExit_onclick()?{
}
//?]]>
?
</script>
</head>
<body>
????<form?id="form1"?runat="server">
????<div>
????????<table?style="width:?515px"?align=center>
????????????<tr>
????????????????<td?colspan="3"?style="height:?23px;?text-align:?center;"?>
????????????????????<span?style="font-family:?華文中宋;?font-weight:?bold;?color:?white;?font-style:?normal;?background-color:?#6699cc;?font-variant:?small-caps;">簡易RSS閱讀器</span></td>
????????????</tr>
????????????<tr>
????????????????<td?style="width:?1241px;?height:?21px;?text-align:?right;">
????????????????????<asp:Label?ID="Label1"?runat="server"?Text="RSS地址:"?Width="113px"?Font-Names="宋體"?Font-Size="Smaller"></asp:Label></td>
????????????????<td?style="width:?8px;?height:?21px"?colspan="2">
????????????????????<asp:TextBox?ID="TextRSSUrl"?runat="server"?
????????????????????????Width="386px"></asp:TextBox></td>
????????????</tr>
????????????<tr>
????????????????<td?colspan="3"?style="text-align:?center">
???????????????????? <asp:Button?ID="ButtonSubmit"?runat="server"?OnClick="ButtonSubmit_Click"?Text="提?交"?/>
????????????????????<asp:Button?ID="ButtonReset"?runat="server"?OnClick="Button1_Click"?Text="重 填"?/>
????????????????????<input?id="ButtonExit"?type="button"?value="退 出"?onclick="window.close();"?/></td>
????????????</tr>
????????</table>
????
????</div>
????</form>
</body>
</html>
?
using?System;using?System.Data;
using?System.Configuration;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Web.UI.HtmlControls;
?
public?partial?class?_Default?:?System.Web.UI.Page
...{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????...{
????}
?
????public?void?ProcessRSSItem(string?rssURL)
????...{
????????System.Net.WebRequest?myRequest?=?System.Net.WebRequest.Create(rssURL);
????????System.Net.WebResponse?myResponse?=?myRequest.GetResponse();
?
????????System.IO.Stream?rssStream?=?myResponse.GetResponseStream();
????????System.Xml.XmlDocument?rssDoc?=?new?System.Xml.XmlDocument();
????????rssDoc.Load(rssStream);
?
????????System.Xml.XmlNodeList?rssItems?=?rssDoc.SelectNodes("rss/channel/item");
?
????????string?title?=?"";
????????string?link?=?"";
????????string?description?=?"";
?
????????for?(int?i?=?0;?i?<?rssItems.Count;?i++)
????????...{
????????????System.Xml.XmlNode?rssDetail;
?
????????????rssDetail?=?rssItems.Item(i).SelectSingleNode("title");
????????????if?(rssDetail?!=?null)
????????????...{
????????????????title?=?rssDetail.InnerText;
????????????}
????????????else
????????????...{
????????????????title?=?"";
????????????}
?
????????????rssDetail?=?rssItems.Item(i).SelectSingleNode("link");
????????????if?(rssDetail?!=?null)
????????????...{
????????????????link?=?rssDetail.InnerText;
????????????}
????????????else
????????????...{
????????????????link?=?"";
????????????}
?
????????????rssDetail?=?rssItems.Item(i).SelectSingleNode("description");
????????????if?(rssDetail?!=?null)
????????????...{
????????????????description?=?rssDetail.InnerText;
????????????}
????????????else
????????????...{
????????????????description?=?"";
????????????}
?
????????????Response.Write("<p><b><a?href='"?+?link?+?"'?target='new'>"?+?title?+?"</a></b><br/>");
????????????Response.Write(description?+?"</p>");
????????}
????}
?
????protected?void?ButtonSubmit_Click(object?sender,?EventArgs?e)
????...{
????????string?RSSURL?=?TextRSSUrl.Text;
????????if?(RSSURL?!=?"")
????????...{
????????????ProcessRSSItem(RSSURL);
????????}
????????else
????????...{
????????????Response.Write(RSSURL);
????????????Response.Write("<font?size=5><b>Site:?"?+?RSSURL?+?"</b></font><Br?/>");
????????????Response.Write("RSS地址不能為空");
????????}
????}
?
????protected?void?Button1_Click(object?sender,?EventArgs?e)
????...{
????????TextRSSUrl.Text?=?"";
????}
}
轉自:http://blog.csdn.net/tsd3698/archive/2007/06/25/1665554.aspx 感謝作者:tsd3698
轉載于:https://www.cnblogs.com/tuyile006/archive/2007/09/10/888282.html
總結
- 上一篇: asp.NET自定义服务器控件内部细节系
- 下一篇: 今天有栋大厦上的玻璃居然可以直接播放电影