SyndicationFeed Class Client With A Proxy Example

.NET 3.5 gave us the Syndication classes which allow you to both create RSS/Atom feed and to consume those feeds.  Which is really confusing – most of the time we have one set of class in the CLR that consume and one that produce.  Having the same object that does both means that when you are browsing sample code on the Internet you need to figure out if the code is client side or server side.  This is client side code – it consumes an atom feed.  The Syndication class are so slick at doing the heavily lifting that you hardly don’t notice when they are not doing what they are suppose to do.  With three lines of code below I can request the atom feed and get it broken down into the SyndicationFeed object.

static void WithOutProxy()
{
    String url = "http://www.31a2ba2a-b718-11dc-8314-0800200c9a66.com/feeds/posts/default";
    XmlReader reader = XmlReader.Create(url);
    SyndicationFeed feed = SyndicationFeed.Load(reader);
}

However, if you want to make a commercial application your code will have to take into account that the computer the product is installed on might be behind a Proxy server.  Here is how to configure the request to use a proxy server and still take advantage of the SyndicationFeed class.  The code above isn’t proxy aware.

static void WithProxy()
{
    String url = "http://www.31a2ba2a-b718-11dc-8314-0800200c9a66.com/feeds/posts/default";

    HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
    httpWebRequest.UserAgent =
        "Googlebot/1.0 (googlebot@googlebot.com http://googlebot.com/)";

    // WWB: Use The Default Proxy
    httpWebRequest.Proxy = System.Net.WebRequest.DefaultWebProxy;

    // WWB: Use The Thread's Credentials (Logged In User's Credentials)
    if (httpWebRequest.Proxy != null)
        httpWebRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;

    using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
    {
        using (Stream responseStream = httpWebResponse.GetResponseStream())
        {
            using (XmlReader xmlReader = XmlReader.Create(responseStream))
            {
                SyndicationFeed syndicationFeed = SyndicationFeed.Load(xmlReader);

                // ...
            }
        }
    }
}

{6230289B-5BEE-409e-932A-2F01FA407A92}

Comments

  1. This was perfect. I don't understand why I couldn't see the nodes in the feed when I used the non-proxy code, since I was able to see everything else, but I can with the proxy-aware version. I have a feeling it has more to do with the useragent. Thanks for this.

    ReplyDelete
  2. Thank you! It's working

    ReplyDelete
  3. still a nice find

    ReplyDelete
  4. Well known intermediaries enable access to the greater part of the basic document sorts, with no confinement. free mexico proxy

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation

Yet once more into the breech (of altered programming logic)

Error : /ScriptResource.axd : Invalid viewstate.