Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Technorati Ping Using C#

0.00/5 (No votes)
29 Jan 2009 1  
Source code to ping technorati for your blogs

Introduction

Anyone that regularly reads or publishes blogs is probably familiar with the concept of pings. If you aren't familiar with the concept of 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 want to create a blogging software then you will have to create the pinging function yourself. Using HTTPPost and C#, I have successfully created a method of doing this using a sample console application. This example uses Technorati but can just as easily be configured for any ping-service site that has an API.

Background

Basic knowledge of C# and doing HttpPost is recommended.

Using the Code

Now that you understand what is ping and the purpose of it, let us see how we implement it in your code. Get the API of the source which you want to notify. Create the web request with the necessary details. For this example, I am adding the Content Length, Content Type. Once you assign all the details, just post and receive the response from the provider via HttpWebResponse.

How It Works ?

Three steps are included in completing the call and getting the response back:

  1. Initialize the request and set its properties.
  2. Send the request to the source using a stream.
  3. Receive and read the response.

Step 1: Initialize the Request and Set its Properties

  • Use the WebRequest.Create method to initialize new HttpWebRequest objects.
  • Now assign the property to "POST".
  • Assign other required parameters such as ContentType, ContentLength and any other additional parameters according to your API specifications.
HttpWebRequest webReq;
webReq = (HttpWebRequest)WebRequest.Create("http://technorati.com/developers/ping");
webReq.Method = "POST";
webReq.ContentType = "text/xml";
webReq.ContentLength = strTechnoratiXML.Length;

Step 2: Send the Request to the Source Using a Stream

Once the initialisation of the request is completed, we need to send this data to the source. To do this, we need to use the "GetRequestStream" method which returns a stream. Then use Stream.write to send the request.

Stream streamRequest = webReq.GetRequestStream();
ASCIIEncoding ascii=new ASCIIEncoding();                
streamRequest.Write(ascii.GetBytes(strTechnoratiXML), 0, strTechnoratiXML.Length);

Step 3: Read the Response

Receive the response into HttpWebResponse using HttpWebRequest.GetResponse method. Now read the response into string with the help of StreamReader.

Once you are done with it, you must call either the Stream.Close or the HttpWebResponse.Close method to close the response and release the connection for reuse. It is not necessary to call both Stream.Close and HttpWebResponse.Close, but doing so does not cause an error.

HttpWebResponse webRes;
StreamReader srResponse;
webRes = (HttpWebResponse)webReq.GetResponse();
srResponse = new StreamReader(webRes.GetResponseStream(), Encoding.ASCII);
String strResponseText = srResponse.ReadToEnd();
srResponse.Close();

Conclusion

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!

It is advised to use configuration file 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.

History

This article has been amended to include more description of the article, which defines what ping is, where it is used and a simple explanation of how to post it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here