Search code examples
c#rssxmlreaderrss-reader

XmlReader Error in reading RSS Feed


I am trying to read an RSS feed from http://backend.deviantart.com/rss.xml?q=gallery:duster132/23316533&type=deviation with the following code:

        //Different RSS Links
        string deviant_rsslink = @"http://backend.deviantart.com/rss.xml?q=gallery:duster132/23316533&type=deviation";
        string blogspot_rsslink = @"http://fightpunch.blogspot.com/feeds/posts/default";

        //Reading the links
        XmlReader reader = XmlReader.Create(deviant_rsslink); //LINE WHERE ERROR OCCURS
        SyndicationFeed feed = SyndicationFeed.Load(reader);
        reader.Close();
        foreach (SyndicationItem item in feed.Items)
        {
            String subject = item.Title.Text;
            Console.WriteLine("Subjext is: " + subject + "\n");
        }

...and I get the error:

"The underlying connection was closed: The connection was closed unexpectedly."

At first I thought deviantart might be blocking my IP so I tried this from different computers with differing IPs however the error persists, so it seems that is not the issue. To make things more difficult to track, the code works with no error at http://fightpunch.blogspot.com/feeds/posts/default.

What should I try to fix this?


Solution

  • You site requires User-Agent header be set

    Below code should work..

    string rss = null;
    using (var wc = new Webclient())
    {
        wc.Headers["User-Agent"] = "SO/1.0";
        rss = wc.DownloadString(deviant_rsslink);
    }
    XmlReader reader = XmlReader.Create(new StringReader(rss));