首页 > 学院 > 开发设计 > 正文

获取网站的RSS聚合到自己的网页

2019-11-18 19:13:52
字体:
来源:转载
供稿:网友

  想把别的网站的rss新闻整合到自己的网页,刚学,一直都不会。今天终于成功了!可能这个方法有点笨!思路是把xml文件下载到本地,然后再读本地XML,就简单了!不知道是否有更简单的方法,希望高手赐教!

核心代码如下:
(用webRequest得到指定页的内容,然后用FileStream写到本地文件中)
核心部份(下载RSS链接的XML文件到本地!)
 
       PRivate void download_Click(object sender, System.EventArgs e)
                {
                        Encoding sjis = Encoding.GetEncoding("utf-8");
                        FileStream fs = new FileStream(Server.MapPath(".")+"//sina.xml",FileMode.Create,Fileaccess.Write);
                        //建立StreamWriter为写做准备
                        StreamWriter rw = new StreamWriter(fs,sjis);
                       
                        string rl;
                                //创建WebRequest对象
                                WebRequest myReq = WebRequest.Create("http://rss.sina.com.cn/news/marquee/ddt.xml");//读取远程文件(新浪新闻),然后用FileStream将文件保存到本地!
                                //创建WebResponse对象
                                WebResponse myRes = myReq.GetResponse();
                                //获取Stream
                                Stream resStream = myRes.GetResponseStream();
                                //以下操作为读取Stream对象中的内容
                                StreamReader sr = new StreamReader(resStream,sjis);
                                StringBuilder sb = new StringBuilder();
                                while((rl=sr.ReadLine())!=null)
                                {
                                        rw.WriteLine(rl);                                       
                                }                               
                        myRes.Close();       
                        rw.Flush();
                        //关闭rw对象
                        rw.Close();
                        fs.Close();
                        msg.Text="下载成功!";

                        }

可以在程序中加入判断,几个小时下一次,或者一天下一次!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表