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.
Last week I published two blog posts about implementing a trackback handler and pingback handler in ASP.NET. I stated that there would be a complementary post about sending trakback and pingback requests in ASP.NET. So here is the final part of these posts about this topic.
Like handling the trackback and pingback requests, there are some differences between sending trackback and pingback requests as well. So I split these implementations into two separate parts to discuss them independently.
Most likely you have a background in the trackback workflow from my post about the implementation of trackback handler. As you can guess, there is an obvious step to parse and extract the trackback handler URI from the RDF code in the destination page content but I’d prefer to ignore this unrelated step here.
The next step is to use basic HTTP verb methods (especially POST verb) to send trackback request and its parameters to trackback handler URI. In .NET this can be accomplished by HttpWebRequest and HttpWebResponse methods. So below code is a general class that lets me send POST requests with my parameters to a specific URL.
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 System.Net;
using System.IO;
using System.Text;
namespace SendTrackbackPingbackSample.Trackback
{
public class HttpClient
{
public string PostRequest(Uri url, string userAgent, int timeout, string formParameters)
{
if (formParameters == null)
throw new ArgumentNullException("formParameters");
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.UserAgent = userAgent;
request.Timeout = timeout;
request.Method = "POST";
request.ContentLength = formParameters.Length;
request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
request.KeepAlive = true;
using (StreamWriter myWriter = new StreamWriter(request.GetRequestStream()))
{
myWriter.Write(formParameters);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode < HttpStatusCode.OK && response.StatusCode >= HttpStatusCode.Ambiguous)
throw new Exception(string.Format(response.StatusCode.ToString()));
var responseText = string.Empty;
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
responseText = reader.ReadToEnd();
}
return responseText;
}
}
}
Having this class, the rest of the process would be pretty simple. As the second step, I implement a TrackbackSender class that applies this HttpClient class in order to send trackback request with four parameters to a destination page. As you may know, there are four parameters that you must pass with your trackback requests:
Thus following class finishes this implementation.
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 System.Text;
namespace SendTrackbackPingbackSample.Trackback
{
public class TrackbackSender
{
#region Properties
public string BlogTitle { get; set; }
public Uri DestinationUrl { get; set; }
#endregion
#region Public Constructor
public TrackbackSender(string blogTitle, Uri destinationUrl)
{
this.BlogTitle = blogTitle;
this.DestinationUrl = destinationUrl;
}
#endregion
#region Public Methods
public void SendTrackback(Uri url, string title, string excerpt)
{
StringBuilder formParams = new StringBuilder();
formParams.AppendFormat("blog_name={0}", HttpUtility.UrlEncode(this.BlogTitle));
formParams.AppendFormat("&url={0}", HttpUtility.UrlEncode(url.ToString()));
formParams.AppendFormat("&title={0}", HttpUtility.UrlEncode(title));
formParams.AppendFormat("&excerpt={0}", HttpUtility.UrlEncode(excerpt));
HttpClient client = new HttpClient();
client.PostRequest(this.DestinationUrl, "Keyvan Nayyeri's Blog Posts",
20000, formParams.ToString());
}
#endregion
}
}
Now that I have all the means necessary for my work, I can send a trackback request in a few lines of code.
protected void btnTrackback_Click(object sender, EventArgs e)
{
TrackbackSender trackback = new TrackbackSender("Sample Blog",
new Uri("http://nayyeri.net/sample-post"));
Uri url = new Uri("http://localhost:10095/");
string title = "Send a Trackback Request in ASP.NET";
string excerpt = "This post describes how to send a trackback request in ASP.NET.";
trackback.SendTrackback(url, title, excerpt);
}
Pingback mechanism is tied to XML-RPC. As pingback handlers are XML-RPC services, you need to use an XML-RPC request to send a pingback request. So here you also need to download and reference XML-RPC.NET library.
The process of this implementation is very similar to the process that you need to follow in order to send requests to any other XML-RPC service. So first you need to build a structure for your service response and its properties.
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;
namespace SendTrackbackPingbackSample.Pingback
{
public struct WeblogsUpdateResponse
{
public bool flerror;
public string message;
}
}
The second step is to build an interface for your client and implements weblogUpdates.ping method on XML-RPC service. This interface has a single method, say Ping, that gets two parameters as your blog or site’s title and your post permalink. Note that the XML-RPC service URL that you need to use for this interface is the URL of pingback handler on destination site that you can extract from HTTP headers or page content.
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 SendTrackbackPingbackSample.Pingback
{
[XmlRpcUrl("http://nayyeri.net/sample-post")]
public interface IWebLogsUpdate
{
[XmlRpcMethod("weblogUpdates.ping")]
WeblogsUpdateResponse Ping(string title, string url);
}
}
If you have a background in XML-RPC requests, you would know that the last step is applying the structure and interface to finalize the implementation. Here PingbackSender class has a single method that sends a pingback request and returns a WeblogsUpdateResponse in response.
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 SendTrackbackPingbackSample.Pingback
{
public class PingbackSender
{
#region Public Methods
public WeblogsUpdateResponse Ping(string title, string url)
{
IWebLogsUpdate rpcProxy = (IWebLogsUpdate)XmlRpcProxyGen.Create(typeof(IWebLogsUpdate));
return rpcProxy.Ping(title, url);
}
#endregion
}
}
Finally I can put these pieces together to send a pingback request in my application.
protected void btnPingback_Click(object sender, EventArgs e)
{
PingbackSender pingback = new PingbackSender();
Uri url = new Uri("http://localhost:10095/");
string title = "Send a Pingback Request in ASP.NET";
WeblogsUpdateResponse response = pingback.Ping(title, url.ToString());
if (response.flerror)
throw new ApplicationException("Pingback Request Failed");
}
As always, the sample source code for this post is available for download.
How to Send Trackbacks and Pingbacks in ASP.NET
Aug 04, 2008 2:58 PM
#
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Hossein
Aug 04, 2008 4:11 PM
#
Hi Keyvan. I know you by a friend of mine. I just want to tell you that your knowledge in .NET is wonderful.
be happy
Brian Lowry
Aug 05, 2008 12:39 AM
#
I wish this article had been around two weeks ago when I was working on trackbacks and pingbacks! Thanks for the great post.
Reflective Perspective - Chris Alcock » The Morning Brew #151
Aug 05, 2008 2:25 AM
#
Pingback from Reflective Perspective - Chris Alcock » The Morning Brew #151
Dew Drop - August 5, 2008 | Alvin Ashcraft's Morning Dew
Aug 05, 2008 8:08 AM
#
Pingback from Dew Drop - August 5, 2008 | Alvin Ashcraft's Morning Dew
Mads Kristensen
Aug 05, 2008 12:00 PM
#
You might want to take a look at this post about how to spam prove your trackback handler http://blog.madskristensen.dk/post/Trackback-spam-fighting.aspx.
I haven't had a single trackback spam using that approach.
Keyvan Nayyeri
Aug 05, 2008 12:04 PM
#
@Mads:
Yes, I can remember that from our work on Subkismet. I don't have any problem with trackback and pingback spam since I'm using Waegis and it blocks them all.
However, thank you pointing it out :-)
Lee
Aug 07, 2008 12:54 AM
#
I'm the same as Brian.. Wish this was around about 5 weeks ago! lol... Thanks for posting it up though, I shall be referencing it a lot today for another project (And will check out Mads link too)
Janko
Aug 08, 2008 4:33 PM
#
Great one, thanks!
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
Amazing web development articles of Summer 2008
Aug 28, 2008 7:58 AM
#
Amazing web development articles of Summer 2008
Bola
Sep 07, 2008 11:25 AM
#
Please can this work on a wordpress blog?
Amazing web development articles of Summer 2008
Oct 20, 2008 2:37 PM
#
The summer is near its end and I think it's a good time to see what was hot during past three months
My Best Blog Posts in 2008
Dec 31, 2008 1:41 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
Rick
Mar 25, 2009 9:45 PM
#
What exactly are your calls to pingback.ping doing? The Pingback specification states that you must pass the source and target URIs in your ping - not a title and URI.
Leave a Comment