I continue my series (I Want More Mobile (Web) Services and Flight Lookup Web Service) of useful Web Services for Windows Mobile with a service to check on package delivery status. If you are like me, you are inpatient when you wait for a package to arrive. I'm almost always waiting for another package to arrive, and when I have a few moments to spare, I come to think of the latest package and want to check where it was last seen. My effort is a growing creature on CodePlex called Windows Mobile Web Services where you get to the code.
As in previous posts, you find the UX on the right, and the client code should be familiar if you've seen the previous posts...
WebServices.Service ws = new WebServices.Service();
resultTextBox.Text = ws.PackageLookup(packageTextBox.Text).Replace("\n", "\r\n");
...and on the server, the code looks like this...
[WebMethod]
public string PackageLookup(string packageNo)
{
// Get DHL tracking page
string url = string.Format("http://www.dhl.com/cgi-bin/tracking.pl?AWB={0}", packageNo);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
Regex regex = new Regex("<a\\s*href=\\x23[0-9]*?><font(.|\\n)*?>(?<number>.*\\n?.*)</font(.|\\n)*?<a\\shref=\\\"" +
"(.|\\n)*?\\\">(?<origin>.*\\n?.*)</a(.|\\n)*?<a\\shref=\\\"(.|\\n)*?\\\">(?<destination>." +
"*\\n?.*)</a(.|\\n)*?face=\\\"arial\\\">(?<status>.*\\n?.*)<img*", RegexOptions.IgnoreCase);
// Extract using regex
string s = null;
Match match = regex.Match(responseData);
if(match.Success)
{
//match.Groups["number"];
s = string.Format("Origin: {0}\nDest.: {1}\nStatus: {2}",
match.Groups["origin"], match.Groups["destination"],
match.Groups["status"].ToString().Replace("<BR>", "\n"));
}
else
throw new Exception("Not found, try again!");
return s;
}
...and as you can see, I'm looking up DHL shipments. It shouldn't be too hard to extend it to cover other couriers as well.
This time I have used a regular expression (regex) to extract the key information from the web page. As you can see in the code above, the code is simpler - if you understand the regex. The regex extracts four pieces of info: the shipping number (which is not used), the package pick up location (origin), the package drop off location (destination), and the current status of the shipment. Regular expression is a very powerful concept that can be used for many things, and it's especially helpful when scraping web pages. My favorite tools for working with regex are Expresso and Regulator, and Regulator is especially useful for .NET development as it's written using .NET (yes, unfortunately there are some minor differences between different implementations).