Archive

Posts Tagged ‘HttpUtility’

Parse a string as URL – HttpUtility

January 29, 2012 1 comment

A pretty neat requirement, automate using an application to go through an unattended mailbox and process the contents. Processing here simply meant to fetch a particular key identifier string the whole shebang that is the mail-body and cross check its presence in the database.

Everything sounded fine, except for the “Fetch a particular identifier”

A part of the entire mail-body was as below:

Identifier XYZ
Commodity Sugar
Date 12-12-2009
http://www.sometempurl.com/page.aspx?id=XYZ

And we had the entire mail-body into a string.

String mail = Message.Body;

One of the developers instinctively suggested, let’s parse our way through “Identifier” substring and fetch the next available value. For me, it sounded a little tacky as the whole thing was in a table and to fetch the cell values based on such an algorithm sounded off to me.

I glanced a little down and my eyes fell on the URL that also had the identifier value XYZ. And as it turns out the mail generator generates the URL with the identifier values as a query string Id. Wah, now it was a piece of cake.

But to again go through the whole substring exercise sounded futile to me. I thought, okay now we have a URL why not programmatically fetch the identifier a query string, rather than parsing our way out. That’s when the following HttpUtility class helped us.

String strURLAddress;

strURLAddress = “http://www.sometempurl.com/page.aspx?id=XYZ”;

HttpUtility.ParseQueryString(strURLAddress)(“id”)

HttpUtility is a class part of System.Web namespace.

Here’s a link to the same tip on CodeProject – http://www.codeproject.com/Tips/304383/Treat-a-plain-string-as-URL-and-fetch-query-string

MSDN Link: http://msdn.microsoft.com/en-us/library/system.web.httputility.parsequerystring.aspx

 

Advertisements
%d bloggers like this: