Sunday, 07 November, 2004
One problem with rolling your own bloging engine is implementing all the fiddly details that other bloggers take for granted. Little things like the RSS syndication or trackback functionality. I developed the RSS feed quite a while ago and it's how most of my vistors keep up-to-date with my posts so I consider the RSS feed a definite success.
The previous blog post about security was going to be a comment on Schneier's new blog but I couldn't get it to format quite the way I wanted it so I thought: "Damn I'm going to have to do a track-back!" Of course, I'm not using Moveable type or some other blogging software so I was going to have to implement this feature myself. Finding the technical specification is a little more difficult than you might have thought due to the fact that when people write articles on how to do track-back pings they write their article for some particular piece of blogging software and not about the way the underlying system works. Never-the-less if you want to go ahead and implement then I found the full spec here.
It's actually pretty easy to send a trackback ping to another web-site. It's essentially a HTTP POST request to a specfic trackback page with the following fields:
title The title of the blog entry excerpt A short teaser from your blog entry url The url to your blog post blog_name The name of the blog in which your entry is contained
The next question is how on earth do you do this in ASP. I must admit, before I took my job in Manchester I would probably have built a custom COM object to achieve this goal, however, there's actually a much easier way to achieve this result. I suspect that what i'm about to show you isn't terribly well known. A lot of the new customers we take on at work struggle making GET/POST requests to other servers when we first open up our specs to them. I get the feeling that most people probably never need to do this and consequently never learn how to do it.
Using the Microsoft XML COM component it only takes a few lines of code. Consider the following:
set oMsXmlHttp = server.createobject("Msxml2.ServerXMLHTTP")
oMsXmlHttp.setTimeOuts [Resolve], [Connect], [Send], [Recieve]
oMsXmlHttp.Open "POST", [Url], False
oMsXmlHttp.Send [Post Request]
sResult = oMsXmlHttp.responseText
The parameters here are pretty straight-forward: The timeouts are measured in milliseconds, the URL is the track-back URL, sResult is the response the track-back page returns to us and the [Post Request] is the actual trackback instruction we send to the track-back URL. This is passed as a typical URL encoded string as per the format given in RFC 1866.
sResult is an XML string returned from the post request track-back page. The XML response is pretty straight forward. The root element is called response. It has two children: the error and message elements. If no error is returned and the track-back was successful then the error element contains "0" and no message element is returned. If there was a problem then the error element contains a "1" and the reason for the failed track-back attempt is returned in the message element.
So there you go.. About fifteen minutes work to add the ability to perform trackbacks to your blogging system. Of course, this won't allow your blog to recieve track-back pings that's a little more complicated but I'll probably cover that when I implement that part of the specification.
Simon.