advanced web statistics

Programmatically Ping Technorati using C#

3/10/2006 3:03:32 PM

Anyone that regularly reads or publishes blogs is probably familiar with the concept of pings. If you aren't familiar with the concept of the ping, at least you've heard of it right?

A pingback (ping for short) is a method by which a blog authoring tool (such as WordPress sends notification to a "ping server" (such as Technorati) informing of new or updated content. The ping is sent via XML-RPC signal and allows the ping server to generate a list of blogs or sites that have new material. This way when surfers visit these sites they will get a listing of the blogs with the latest content.

Most of the existing blog-authoring tools have built-in pinging technology.  If you are like me and want to create your own blogging software then you will have to create the pinging function yourself.  Using XML and C# I have successfully created a method of doing this.

This example uses Technorati but can just as easily be configured for any ping-service site that has an API.

private void UpdatePing()
{
   string ping = "http://rpc.technorati.com/rpc/ping";
   string url = "http://www.willasrari.com/blog";
   string name = "Will Asrari";

   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ping);
   request.Method = "POST";
   request.ContentType = "text/xml";

   (using Stream stream = (Stream)request.GetRequestStream())
   using (XmlTextWriter xml = new XmlTextWriter(stream, Encoding.UTF8))
   {
      xml.WriteStartDocument();
      xml.WriteStartElement("methodCall");
      xml.WriteElementString("methodName", "weblogUpdates.ping");
      xml.WriteStartElement("params");
      xml.WriteStartElement("param");
      xml.WriteElementString("value", name);
      xml.WriteEndElement();
      xml.WriteStartElement("param");
      xml.WriteElementString("value", url);
      xml.WriteEndElement();
      xml.WriteEndElement();
      xml.WriteEndElement();
   }
}

This code is pretty self-explanatory. It is worth mentioning that you should use the System.Xml namespace. After implementing this code in your site you will have an edge over those without pingback capabilities by getting listed at the top-of-the-pile!

You could also use XML configuration to customize the URL, description, and blog names so that you don't have to hardcode them into your function.  Also, with a little bit of editing, you could loop through a list of services that allow you to programmatically send pingbacks.  I'll probably expand on this a bit later.

C#, Code, XML

kick it on DotNetKicks.com

Comments


Great lines of code ... !!! I`ll put a reference in my blog to your article, and you have a small bug in the line

xmlTechnoratiPing.Close();

must be

xmlPing.Close();

Great work again !!!

Bye from Spain

Posted by: El Bruno | 8/3/2006 4:49:08 AM

Great piece of code. This was very handy. I converted the code to VB.NET and implemented it on my site with modifications to add other sites to ping. A VB.NET version of this code can be found at http://www.jeremywadsworth.com/Default.aspx?blogentryid=29

Thanks again.

Posted by: Jeremy Wadsworth | 10/5/2006 8:23:27 PM

Leave a Comment

   

  Enter the text to proceed!