Pinging Technorati using C#

2/4/2009

One of the features that I liked about BlogEngine.Net was the fact that it pings various aggregators for you. To be honest, I had no idea at the time how this was accomplished. Then again, at the time I didn't know how to create an RSS feed either but that has since changed.

For those curious, sites like Technorati use something called XML-RPC to allow computers to tell these services when they've been updated. To be honest, all it is, is an XML file that is posted to a specific address. The XML file contains the name of the site and the address. Once the service has that information, it crawls the site again looking for updates... I have no idea why you wouldn't simply put in the pages that have been added/changed in a list, but I didn't come up with the format. As always, I wanted to come up with some code to accomplish this for me. And as always, I'm here to share it with you:

   1: public static void PingServices(List<Uri> Services,Uri Blog,string BlogName)
   2: {
   3:     foreach (Uri Service in Services)
   4:     {
   5:         HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(Service);
   6:         Request.Credentials = CredentialCache.DefaultNetworkCredentials;
   7:         Request.ContentType = "text/xml";
   8:         Request.Method = "POST";
   9:         Request.Timeout = 10000;
  10:         Stream Stream = (Stream)Request.GetRequestStream();
  11:         using (XmlTextWriter XMLWriter = new XmlTextWriter(Stream, Encoding.ASCII))
  12:         {
  13:             XMLWriter.WriteStartDocument();
  14:             XMLWriter.WriteStartElement("methodCall");
  15:             XMLWriter.WriteElementString("methodName", "weblogUpdates.ping");
  16:             XMLWriter.WriteStartElement("params");
  17:             XMLWriter.WriteStartElement("param");
  18:             XMLWriter.WriteElementString("value", BlogName);
  19:             XMLWriter.WriteEndElement();
  20:             XMLWriter.WriteStartElement("param");
  21:             XMLWriter.WriteElementString("value", Blog.ToString());
  22:             XMLWriter.WriteEndElement();
  23:             XMLWriter.WriteEndElement();
  24:             XMLWriter.WriteEndElement();
  25:         }
  26:         Request.GetResponse();
  27:     }
  28: }

That's all there is to it. The code above will take in a list of services (Technorati uses http://rpc.technorati.com/rpc/ping but this code will work with other services), the URI of your website and the title of it and send this information to the services. Depending on the network of your server, you may want to increase the timeout from 10 seconds to something else but I find that 10 seconds seems to work most of the time. Hopefully this helps someone out. So try it out, leave feedback, and happy coding.



Comments