Click here to Skip to main content
Click here to Skip to main content

Technorati Ping Using C#

, 29 Jan 2009
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ravi Vooda
Software Developer (Senior) ValueLabs
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 2 PinmemberMember 19923188-Nov-10 17:38 
GeneralVS Pinmemberblackseth31-Jan-09 23:57 
GeneralMy vote of 1 PinmemberHaluk_YILMAZ29-Jan-09 22:36 
GeneralRe: My vote of 1 PinmemberRavi Vooda30-Jan-09 23:33 
GeneralRe: My vote of 1 PinmemberHaluk_YILMAZ31-Jan-09 12:01 
GeneralAaaah... PinmemberPIEBALDconsult29-Jan-09 10:55 
GeneralRe: Aaaah... PinmemberRavi Vooda29-Jan-09 16:17 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 29 Jan 2009
Article Copyright 2009 by Ravi Vooda
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid