I wanted to add my Facebook status to my other blog which is running Community Server 2.0. All I needed to do was write a user control to pull the RSS feed and display the text. This seemed easy enough, but it turned out to be a little tricky.

First I tried to use the XmlDocument class and load the RSS URL directly. That complained about a missing DTD which was confusing since the URL works fine in a browser. After some debugging, I discovered that Facebook sniffs your User Agent. Since the XmlDocument was sending an unknown user agent field, Facebook was replying with this page.

I changed my tactics a bit and used the HttpWebRequest object which allows me to specify the user agent field. After that it was relatively simple to load the response stream into an XmlDocument, use an XPath query to find the node I wanted, and load the status text into a literal control. Feel free to copy this code if you have run into a similar problem.

<%@ Import namespace="System" %>
<%@ Import namespace="System.IO" %>
<%@ Import namespace="System.Net" %>
<%@ Import namespace="System.Xml" %>
<%@ Import namespace="System.Web.UI.HtmlControls" %>
<%@ Import namespace="System.Web.UI.WebControls" %>
<%@ Import namespace="System.Web" %>
<%@ OutputCache Duration="600" VaryByParam="none" %>
<%@ Control Language="vb" AutoEventWireup="true" EnableViewState="false" Debug="false" %>

<script runat="server" language="vb">
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ltlFacebook.Text = ""
        Dim sReader as StreamReader
        Dim resp as HttpWebResponse
        Try
            Dim req as HttpWebRequest = CType(WebRequest.Create("http://www.facebook.com/feeds/status.php?id=584666720&viewer=584666720&key=12cf50d10b&format=rss20"), HttpWebRequest)
            req.UserAgent = "Windows-RSS-Platform/1.0 (MSIE 7.0; Windows NT 5.1)"
            resp = CType(req.GetResponse(), HttpWebResponse)
            sReader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8)
            Dim xmldoc As New XmlDocument
            xmldoc.LoadXml(sReader.ReadToEnd())
            Dim node as XmlNode = xmlDoc.SelectSingleNode("/rss/channel/item/title")
            ltlFacebook.Text = node.InnerText
        Finally
            sReader.Close()
            resp.Close()
        End Try
    End Sub
</script>
<asp:Literal id="ltlFacebook" runat="server"></asp:Literal>