|
Recent Articles |
A New Way to Organize Your Feeds When you come across something interesting on the web, but don't have time to read it at that moment, what do you do?The old way is to add the web page to your browser's bookmarks or favourites so you can retrieve it when you do have time. A more recent method is to...
Google Sitemaps and Competitive Intelligence I'm a big fan of the Google Webmaster Central Program and using sitemaps. I agree that you should build your website so that it is crawlable and not rely on sitemaps to compensate for poor site architecture, but...
A Milestone for XBRL This could be a milestone in illustrating the benefits of using XBRL for companies filing financial data.Last week, Microsoft submitted a Form 8-K filing to the financial data.Last week, Microsoft submitted a Form...
When Is XML Not XML? Here is a mystery for folks. I've updated my parsing engine for coldfusionbloggers.org. I'm using CFHTTP now so I can check Etag type stuff. I take the result text and save it to a file to be parsed by CFFEED.
|
|
03.04.08
XML-RPC ping endpoint in C# and ASP.NET
By Mads Kristensen
All blog platforms send out pings using the XML-RPC protocol whenever a new post is created or an old one is updated. It is very simple to send out XML-RPC pings using C#, because it is just a normal HTTP request with some XML in the request body. See here how to ping using C#.
To send a ping is the client part of the transaction. There must also be a server to intercept the ping – an endpoint. Feedburner is probably the most widely used by blogs at the moment. What happens is that when you write a post, the blog engine sends a ping to Feedburner’s XML-RPC endpoint. In the XML body of the ping request is the URL of your blog so Feedburner knows who sent the ping. It then retrieves your blog’s RSS feed, parses it, and spits it out to your readers. That’s how simple and powerful it is to use XML-RPC pings.
In an earlier post I showed you how to write the ping client, now let’s look at the server or endpoint. The endpoint can be used by any service that needs to be notified whenever content is updated on a website. That goes for all applications that listen to RSS feeds for instance.
The Code
In 100 lines of code including comments, we are able to build an XML-RPC ping server endpoint. All it has to do is to listen for POST requests and parse the XML sent in the request body as shown in my earlier post. By parsing the XML it can find the URL that has been updated. The method HandleUrl is where the URL is passed to and it's from here you write your logic to handle the ping request.
#region Using
using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Web;
#endregion
public class XML_RPC : IHttpHandler
{
/// <summary>
/// Handles the URL that was sent by the ping request.
/// </summary>
private void HandleUrl(Uri url)>
{
//This is where you write the code for handing the incoming URL.
//It is the only place you need to change.
HttpContext.Current.Response.Write(url);
}
#region Parse the request
/// <summary>
/// Parses the request body and returns the URL.
/// </summary>
private static string ParseXml(string xml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string methodName = doc.SelectSingleNode("//methodName").InnerText;
if (methodName == "weblogUpdates.ping")
{
XmlNodeList values = doc.SelectNodes("//value");
if (values.Count == 2)
{
return values[1].InnerText;
}
}
throw new NotSupportedException("'" + methodName + "' XML-RPC method not supported");
}
/// <summary>
/// Receives this content from the current HTTP request stream.
/// </summary>
public static string ReceiveBody()
{
using (StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream))
{
return reader.ReadToEnd();
}
}
Continue reading this article...
About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.
|