I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering and Programming.
One of my yesterday’s blog posts was about implementing a trackback handler in ASP.NET and I stated that I will write a separate post about implementing a pingback handler as well.
Even though trackback and pingback may look like similar concepts to ordinal users but they’re somewhat technically different. However, when reading this post, you will notice some commonalities between these two concepts and their implementations.
Pingback is built on top of XML-RPC web services and its infrastructure is very similar to MetaWeblog API that I had covered before.
Pingback is a common concept on the web among site owners and bloggers and is used to do the same job as trackback does. Pingback connects a destination site to the source site that has referred to it. This way, readers can find relevance content easily. Pingbacks are stored in the same way as trackbacks and sometimes there may not be any noticeable difference between them like what is provided in Graffiti.
The infrastructure of pingback is built on top of XML-RPC services and there is a workflow behind them that I describe here shortly:
Like trakcbacks, pingbacks are also commonly implemented by many of the common blogging engines so you wouldn’t worry about anything unless you want to implement your own blogging engine or site.
Before talking about more details of these steps, let me point that you need to download and install XML-RPC.NET library to be able to use the sample code provided in this blog post.
As you saw, one of the first steps is to add an HTTP header to your site or provide a link with appropriate rel attribute in your pages.
The preferred method is HTTP header and you need to set an X-Pingback header to the URI of your pingback service.
Below code adds such a header to an ASP.NET page.
protected void Page_Load(object sender, EventArgs e)
{
Response.AddHeader("X-Pingback", "http://localhost:10220/Pingback.aspx");
}
The other way to provide information about pingback service on your end is, adding a <link /> element to your page headers with a rel attribute set to pingback value and an href attribute set to the URI of your pingback service. Here you can see an example:
<head runat="server">
<title>Sample Page</title>
<link rel="pingback" href="http://localhost:10220/Pingback.aspx">
</head>
The next step, as you guess, is creating an ASP.NET HTTP handler that will act as an XML-RPC service. Here I create a PingbackHandler class and derive it from XmlRpcService included in XML-RPC.NET library. This will turn my class to an XML-RPC service.
I also need to define my HTTP handler in Web.Config as is shown below.
<httpHandlers>
<add verb="*" path="Pingback.aspx" type="PingbackSample.PingbackHandler, PingbackSample"/>
</httpHandlers>
But the main part of pingback implementation is, writing the XML-RPC service. Thank to XML-RPC.NET library, this is pretty simple and easy to achieve. You need to provide an XML-RPC method called pingback.ping that gets two string parameters as the URI of the source page and the URI of the target page and returns a string value that usually contains a success text or failure text.
Also you usually need to parse the destination page URI in order to extract some parameters like post identifier or post name to be able to store the pingback request for the relevance local item. Often blog engines try to retrieve the title of the source page and store it among the source page URI in their local storage.
So here is the code for my implementation of this pingback handler:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CookComputing.XmlRpc;
namespace PingbackSample
{
public class PingbackHandler : XmlRpcService
{
[XmlRpcMethod("pingback.ping")]
public string pingback(string sourceUri, string targetUri)
{
try
{
// Find the item ID or item name
// Check for the existence of a page at source URI
// Check for the existence of a link to the target URI in the source URI
// Store the pingback on local server for the appropriate item
}
catch
{
throw new XmlRpcFaultException(1, "Invalid sourceUri parameter.");
}
return "Your ping request has been received successfully.";
}
}
}
Note that I have left some code as comments for you to implement it based on your own code. Like trackback, pingback is a good candidate for spammers as well and this is the main reason that you need to check and confirm the existence of the source URI and the inclusion of your destination URI in it.
The final response of this method is a simple text string that doesn’t play a very important role in the process.
That’s all about pingback handling in ASP.NET! You see that they are a little easier to implement than trackbacks. I may write a post about sending trackback and pingback requests from your ASP.NET applications in the near future.
You can grab the source code sample of this post from here.
Reflective Perspective - Chris Alcock » The Morning Brew #149
Aug 01, 2008 2:28 AM
#
Pingback from Reflective Perspective - Chris Alcock » The Morning Brew #149
Dew Drop - August 1, 2008 | Alvin Ashcraft's Morning Dew
Aug 01, 2008 8:56 AM
#
Pingback from Dew Drop - August 1, 2008 | Alvin Ashcraft's Morning Dew
How to Send Trackback and Pingback Requests in ASP.NET
Aug 04, 2008 2:55 PM
#
Last week I published two blog posts about implementing a trackback handler and pingback handler in ASP
MetaWeblog, Trackback and Pingback in ASP.NET « vincenthome’s Software Development
Aug 14, 2008 11:12 PM
#
Pingback from MetaWeblog, Trackback and Pingback in ASP.NET « vincenthome’s Software Development
My Best Blog Posts in 2008
Dec 31, 2008 1:40 PM
#
In the past 3.5 years of blogging, I haven’t had such best pick up collections in the end of the year, but now that everybody is writing one, why shouldn’t I write my own?! Collecting this list, I could realize some interesting facts that completely changed
Leave a Comment