Submit Your Article
Home Articles News Tutorials Videos Add An Article
Topics: Design Photoshop Programming PHP CSS Java Database Web Development Javascript Ajax
– Close + Open

Find Out More About DevWebPro!

Sign up for the newsletter


» Terms & Conditions

Welcome to the New DevWebPro!

DevWebPro Includes:
  Hundreds Of Tutorials   Developer News
  Unique Gadget Videos   Tons of Topics to Discuss
  Expert Advice   We Will Publish Your Articles

Ping Using XML-RPC In ASP.NET

By: Mads Kristensen
Tuesday, November 20th, 2007
Text: Decrease Font Size Increase Font Size | Print Print Article | Share: Delicious Digg StumbleUpon Post to Twitter Post to Facebook

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();
}
}

Topics: , , ,

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. http://www.madskristensen.dk/
DevWebPro is an iEntry Network ® publication - © 1998-2010 All Rights Reserved