Joshua Stengel

Everything...Is going...
To be okay!

July 2008 - Posts

UserAgent Fix for the RSSToolkit 2.0.0.0

I frequently use the RSSToolkit to consume and display RSS feeds from other blogs.  Recently, I've noticed problems with the RSSToolkit not being able to display feed from some external sources.  In particular, I've been having trouble consuming feeds from external blogs like Blogspot and Feedburner.  The symptoms appeared over the past month or two and included the disappearance of consumed blogs and new blog posts.

After some digging, I found this entry on Obishan's blog that gave me a clue. Using Web-Sniffer, I was able to verify that if no UserAgent was provided for the request, the request was redirected (302).  This redirection seems to the source of my problems with the RSSToolkit and I'm not the only one.

This wasn't my first crack at fixing an issue with the RSSToolkit, so I spent some time tracking done how to fix this issue. Basically, I needed to find out where the RSSToolkit was actually requesting the RSS and find out how to modify the request so that it included a UserAgent. In my fix I am hardcoding in the UserAgent.  Ideally, this would be set through a property in the control.

The Fix

Step 1: Download the Source

Step 2: Update the Code

Open RSSToolkit > RSS > DownloadManager.cs and replace the DownloadFeedDom() with the following:

private CacheInfo DownLoadFeedDom(string url)
        {
            //// look for disk cache first
            CacheInfo dom = TryLoadFromDisk(url);

            if (CacheReadable(dom))
            {
                return dom;
            }

            string ttlString = null;

            //MODIFICATION: Some RSS sources like Facebook and Feedburner look
            //for a UserAgent (browser/OS information)

            String userAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.UserAgent = userAgent;
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            XmlDocument doc = new XmlDocument();
            doc.Load(httpResponse.GetResponseStream());
            httpResponse.Close();
            doc.Save(Console.Out);

            //END OF MODIFICATION

            if (doc.SelectSingleNode("/rss/channel/ttl") != null)
            {
                ttlString = doc.SelectSingleNode("/rss/channel/ttl").Value;
            }

            //// compute expiry
            int ttlMinutes = GetTtlFromString(ttlString, _defaultTtlMinutes);
            DateTime utcExpiry = DateTime.UtcNow.AddMinutes(ttlMinutes);

            //// save to disk
            return TrySaveToDisk(doc, url, utcExpiry);
        }

Step 3: Rebuild the Project and use the new RssToolkit.dll

(Optional) Step 4: Download my copies.  My versions also include my fix for Atom feeds.

More Posts