Ping using XML-RPC in ASP.NET

by Mads Kristensen 1. August 2007 22:17

Many blogs have the ability to ping different ping-services, such as Ping-o-Matic, Feedburner and Technorati, whenever some content is created or updated. But it is not only blogs who can benefit from pinging these services. Almost all websites that is updated regularly can use this technique.

All these services use XML-RPC and the exact same format, so you can write a ping class ones and then just add whatever ping service URL later. I’ve written a very simple static ping class that can be used in any ASP.NET application.

The code

Here is the the three methods needed to send XML-RPC pings.

/// <summary>
/// Sends a ping to various ping services.
/// </summary>
public static void Send()
{
  Execute("http://ping.feedburner.com");
  Execute("http://rpc.pingomatic.com/RPC2");
}

/// <summary>
/// Creates a web request and with the RPC-XML code in the stream.
/// </summary>
private static void Execute(string url)
{
  try
  {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    request.ContentType = "text/xml";
    request.Timeout = 3000;

    AddXmlToRequest(request);
    request.GetResponse();   
  }
  catch (Exception)
  {
    // Log the error.
  }
}

/// <summary>
/// Adds the XML to web request. The XML is the standard
/// XML used by RPC-XML requests.
/// </summary>
private static void AddXmlToRequest(HttpWebRequest request)
{
  Stream stream = (Stream)request.GetRequestStream();
  using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.ASCII))
  {
    writer.WriteStartDocument();
    writer.WriteStartElement("methodCall");
    writer.WriteElementString("methodName", "weblogUpdates.ping");
    writer.WriteStartElement("params");
    writer.WriteStartElement("param");
    // Add the name of your website here
    writer.WriteElementString("value", "The name of your website");
    writer.WriteEndElement();
    writer.WriteStartElement("param");
    // The absolute URL of your website - not the updated or new page
    writer.WriteElementString("value", "http://www.example.com");
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteEndElement();
  }
}

Implementation

Download the class below and drop it into the App_Code folder or a class library. Then from anywhere in your ASP.NET project you can use the class by calling the Send method like so:

PingService.Send();

Because it can take some time to ping all the different services, you might want to consider doing it asynchronously. Here is how to do that. That’s it. Now you have a class that pings various services using XML-RPC. You can find a full list of available ping services here.

PingService.zip (816 bytes)

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags:

ASP.NET

Comments

8/1/2007 11:12:06 PM #

Josh Stodola

Thanks a bunch for sharing this with us, Mads!!

Josh Stodola United States |

8/2/2007 5:48:06 PM #

Dan Atkinson

That's a damned useful bit of code there Mads!

Kudos!

Dan Atkinson United Kingdom |

8/2/2007 5:53:36 PM #

Korayem

...and the point of not-limiting XML-RPC to blogging is wicked.

Korayem Egypt |

8/2/2007 9:35:38 PM #

pingback

Pingback from blog.veggerby.dk

public class Veggerby : IBlog » Writing a pingback server and client

blog.veggerby.dk |

8/2/2007 11:13:35 PM #

pingback

Pingback from mhinze.com

13 Links Today (2007-08-02)

mhinze.com |

8/4/2007 3:00:41 AM #

brady gaster

Thanks for sharing this, Mads!

brady gaster United States |

8/4/2007 7:52:36 PM #

Welbin

That's great

Welbin People's Republic of China |

8/25/2007 4:15:48 AM #

trackback

Trackback from mikedopp.com

Mikes Links Monday 8/27/07

mikedopp.com |

8/29/2007 12:46:36 AM #

Cristiano

I'm sorry for my very poor english. I'm using blogengine.net on my site: it's a cool project.

I have rewrited your code in VBScript and i have published an article on my site regard this interesting post and more at www.cristianofino.net/post.aspx

Best regards.

Cristiano Italy |

3/17/2008 8:13:54 PM #

pingback

Pingback from fireyang.v255.com

FireYang’s Blog » 在 ASP.NET中通过 XML-RPC 进行Ping(译)

fireyang.v255.com |

12/18/2008 9:27:47 AM #

bedava ödev

Hello bro. Its a really nice post. But i have a little question for you if you dont mind.

I tried your code in my project : and when i debug it step by step i saw that all the ping services return "request time-out". I didnt understand why is that hapenning. And Secondly I just ping my absolut url to them, so how do they know my rss.xml 's location ?

if you can answer my questions asap i would be very happy..
thanx again.

bedava ödev Turkey |

1/10/2009 2:36:53 AM #

online dating

The Weblogs.com ping service is used to automatically inform VeriSign whenever you update content on your site. The service receives notification (a "ping") from your site that you have added new content, and if all goes well, Weblogs.com adds your site to a list of recently changed weblogs.

online dating United Kingdom |

6/16/2009 3:13:00 PM #

pingback

Pingback from blog.dmbcllc.com

Programming SEO – Ping

blog.dmbcllc.com |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008