Home > Technology > Parse a string as URL – HttpUtility

Parse a string as URL – HttpUtility

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

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 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


  1. February 4, 2012 at 11:03 AM

    superb post! Keep up the nifty work!

  1. No trackbacks yet.

If you have something to say about the post, please do. Positive criticism and comments always welcome!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: