Click here to Skip to main content
15,883,883 members
Articles / Programming Languages / C#

Technorati Ping Using C#

Rate me:
Please Sign up or sign in to vote.
3.89/5 (7 votes)
29 Jan 2009CPOL3 min read 37.5K   440   18   7
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.
C#
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.

C#
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) ValueLabs
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Gophern Kwok8-Nov-10 17:38
Gophern Kwok8-Nov-10 17:38 
GeneralVS Pin
blackseth31-Jan-09 23:57
blackseth31-Jan-09 23:57 
GeneralMy vote of 1 Pin
Haluk_YILMAZ29-Jan-09 22:36
Haluk_YILMAZ29-Jan-09 22:36 
GeneralRe: My vote of 1 Pin
Ravi Vooda30-Jan-09 23:33
Ravi Vooda30-Jan-09 23:33 
GeneralRe: My vote of 1 Pin
Haluk_YILMAZ31-Jan-09 12:01
Haluk_YILMAZ31-Jan-09 12:01 
GeneralAaaah... Pin
PIEBALDconsult29-Jan-09 10:55
mvePIEBALDconsult29-Jan-09 10:55 
GeneralRe: Aaaah... Pin
Ravi Vooda29-Jan-09 16:17
Ravi Vooda29-Jan-09 16:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.