OtherCode /AsyncRss/RssPage.aspx.cs

Language C# Lines 104
MD5 Hash ba3b91b23050d7fc5f0445080436e9dd Estimated Cost $2,055 (why?)
Repository https://github.com/hmol/OtherCode.git View Raw File
  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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Configuration;
using System.IO;
using System.Net;
using System.Web.Script.Serialization;
using System.Xml.Linq;

public partial class RssPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string DownloadRssFeed(string inputUrl)
    {
        if (string.IsNullOrWhiteSpace(inputUrl))
            return string.Empty;

        var rssUrl = inputUrl;
        if (string.IsNullOrWhiteSpace(rssUrl))
            return string.Empty;

        try
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(rssUrl);

            using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
            {
                using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
                {
                    var feed = XDocument.Load(new StringReader(streamReader.ReadToEnd()));

                    var rssDeclaration = feed.Descendants("rss");

                    var rssFeedResult = new List<RssFeedItem>();

                    foreach (var item in feed.Descendants("item"))
                    {
                        if (item == null) continue;
                        var title = item.Element("title");
                        if (title == null || string.IsNullOrWhiteSpace(title.Value)) continue;
                        var url = item.Element("link");
                        if (url == null || string.IsNullOrWhiteSpace(url.Value)) continue;

                        rssFeedResult.Add(new RssFeedItem()
                        {
                            Title = title.Value,
                            Url = url.Value
                        });
                    }

                    return new JavaScriptSerializer().Serialize(rssFeedResult);
                }
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string GetRssItems()
    {
        var feedList = new List<RssFeedItem>();


        feedList.Add(new RssFeedItem(){ 
                        Title="Le Monde" ,
                        Url = "http://rss.lemonde.fr/c/205/f/3061/index.rss"
        });
        feedList.Add(new RssFeedItem()
        {
            Title = "New York Times",
            Url = "http://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml"
        });
            
        feedList.Add(new RssFeedItem()
        {
            Title = "Shanghai daily",
            Url = "http://rss.shanghaidaily.com/Portal/mainSite/handler.ashx?template=template&filename=latestnews&foldername=mainSite&sid=id"
        });


        return new JavaScriptSerializer().Serialize(feedList);
    }

    private class RssFeedItem
    {
        public string Url { get; set; }
        public string Title { get; set; }
    }
}
Back to Top