sites i read
Admin
C# Trackback, Part 1
i’ve been wanting to use this blog for discussions about technology, programming specifically, though it’s obvious i just like to blather as well.
so here’s my first snippet.
this is the Trackback Ping i use for this site. It’s nothing fancy, and it could probably use some cleanup, but i’m posting nonetheless.
here goes:
public string SendTrackbackPing( string pingUrl, string url, string title, string exerpt, string blogName ) { //validate that we at least have the url //much more could be done to verify a proper url format, but if it's bad, it'll just fail //garbage in, garbage out. if (url == null) { throw new Exception("A url is required to send a trackback ping."); } if (pingUrl == null) { throw new Exception("A ping url is required to send a trackback ping."); } //build the form data //example: title=Foo+Bar&url=http://www.bar.com/&excerpt=My+Excerpt&blog_name=Foo StringBuilder sb = new StringBuilder(); sb.Append("url="); sb.Append(encode(url)); sb.Append("&"); if (title != null) { sb.Append("title="); sb.Append(encode(title)); sb.Append("&"); } if (exerpt != null) { sb.Append("exerpt="); sb.Append(encode(exerpt)); sb.Append("&"); } if (blogName != null) { sb.Append("blog_name="); sb.Append(encode(blogName)); sb.Append("&"); } //post to the pingUrl HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(pingUrl); httpRequest.ContentType = "application/x-www-form-urlencoded"; httpRequest.Method = "POST"; byte[] postByteArray = Encoding.ASCII.GetBytes(sb.ToString()); httpRequest.ContentLength = postByteArray.Length; Stream stream = httpRequest.GetRequestStream(); stream.Write(postByteArray, 0, postByteArray.Length); stream.Close(); //get a response. it <strong>should</strong> be an xml reponse HttpWebResponse httpResponse = null; try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); } catch (Exception oops) { //int code = (int) httpResponse.StatusCode; throw new Exception("Unable to connect to '" + pingUrl + "'. " + oops.ToString()); } StreamReader responseStream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.GetEncoding(1252)); string response = responseStream.ReadToEnd(); responseStream.Close(); httpResponse.Close(); //alrighty, i need to parse the response. it's xml. //i could ignore that and just do some regex //or i could accept it and do xpath //i think regex will be lighter string errorCode = null; string errorMessage = null; Regex responseReg = new Regex(@"<error>(?<errorcode>\d{1})</error>", RegexOptions.IgnoreCase); if (responseReg.IsMatch(response)) { errorCode = responseReg.Match(response).Groups["errorcode"].Value; } if (errorCode == null) { return "Error: \n\n" + response; } if (errorCode == "0") { responseReg = null; return "success"; } else { responseReg = new Regex(@"<message>(?<errorMessage>[\w\W]*)</message>", RegexOptions.IgnoreCase); if (responseReg.IsMatch(response)) { errorMessage = responseReg.Match(response).Groups["errorMessage"].Value; } } responseReg = null; if (errorCode != "0" && errorMessage == null) { throw new Exception("Error parsing response from trackback ping: Unable to determine error message."); } return errorMessage; } //encode strings for a happy POST private string encode( string input ) { if (input null || input "$4") { return ""; } string newValue = input.Replace("\"", "'"); newValue = System.Web.HttpUtility.UrlEncode(newValue); newValue = newValue.Replace("%2f", "/"); return newValue; }
i call it from my article edit screen via ajax, but it could just as well be called from a regular form post, or from some other method if you like.
soak it in. use it as you want.
i’m going on vacation for a few days, so if you post, it won’t show on the site until i get back to moderate, but it won’t be lost.
some time after i return, i’ll post Part 2, which will be the Trackback receiver, for lack of a better term.
enjoy.
UPDATE – Part 2 has been published.