|
Technology
|
|
Ping Feedburner, Ping-o-Matic, Google, Technorati and All the Other Cool Kids Using ASP.NET and Some Magic
|
|
|
|
|
|
|
|
|
|
|
|
It has been a long time coming, but we have finally managed to migrate away from Community Server to our own custom blog software that is integrated directly into the web site. The reasons for the change are numerous including the need to customize the look and navigation of our blogging software and our desire to provide readers a more streamlined experience.
The final part of the migration to this new system was released tonight. This final piece being the ability to ping the various providers that watch new blog posts. The code required to do this is not difficult to write. I am posting it below in case anyone is interested in how it is done.
Ironically enough this post will be the first one to actually use the ping service. Here's a link that helped me figure out how to do the ping and here's the code:
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using ThePlanCollectionCore.Utils;
namespace ThePlanCollectionCore.DataFeeds
{
/// <summary>
/// Wrapper for xml rpc service
/// that can ping ping-o-matic and other services
/// when your blog is updated.
/// </summary>
static public class Ping
{
// List of services that accept an xml rpc ping.
// If you surf around the net you will find many other
// services but many of them are offline or
// appear to be junk. Stick with the main ones.
static private string [] _Services = {
"http://rpc.pingomatic.com",
"http://blogsearch.google.com/ping/RPC2",
"http://www.blogsnow.com/ping",
"http://www.wasalive.com/ping/"
};
/// <summary>
/// Sends a ping to the above services using the given website name
/// and url
/// </summary>
/// <param name="websiteName">The name of the website or blog
/// that has been updated.</param>
/// <param name="websiteUrl">The url of the website or blog
/// that has been updated. This should be an absolute root url
/// not a direct link to the individual page that has been updated.</param>
public static void Send(string websiteName, string websiteUrl)
{
for (int i = 0; i < _Services.Length; i++)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_Services[i]);
request.Method = "POST";
request.ContentType = "text/xml";
request.Timeout = 3000;
// Add in the updated page
BuildRequest(request, websiteName, websiteUrl);
// Send off the request
request.GetResponse();
}
catch (Exception ex)
{
// Log the error.
Error err = new Error();
err.Title = "Ping Request Failure";
err.Body = "Ping to " + _Services[i] + " failed with error: " + ex.Message;
err.Save();
}
}
}
/// <summary>
/// Adds the requred xml to the request. This includes the website name and url
/// along with the method name 'ping' that will tell the remote servers that
/// and update has been made
/// </summary>
/// <param name="request">A http webrequest generated by the method 'Send' above.</param>
/// <param name="websiteName">The name of the website or blog
/// that has been updated.</param>
/// <param name="websiteUrl">The url of the website or blog
/// that has been updated. This should be an absolute root url
/// not a direct link to the individual page that has been updated.</param>
private static void BuildRequest(HttpWebRequest request, string websiteName, string websiteUrl)
{
Stream stream = request.GetRequestStream();
using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.ASCII))
{
writer.WriteStartDocument();
writer.WriteStartElement("methodCall");
writer.WriteElementString("methodName", "ping");
writer.WriteStartElement("params");
writer.WriteStartElement("param");
// Add the name of your website here
writer.WriteElementString("value", websiteName);
writer.WriteEndElement();
writer.WriteStartElement("param");
// The absolute URL of your website - not the updated or new page
writer.WriteElementString("value", websiteUrl);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
}
}
}
}
|
|
|