To parse a Uri (Uniform Resource Identifier) into its constituent parts, .NET Framework provides System.Net.Uri class that can be used to do the job. Construct a System.Net.Uri object and pass the URI to the constructor. This class constructor parses out the constituent parts of the URI and allows access to them via the Uri properties. We can then display the URI pieces individually:
public static void ParseUri(string uriString) { try { // just use one of the constructors for the System.Net.Uri class // this will parse it for us. Uri uri = new Uri(uriString); // Look at the information we can get at now... string uriParts; uriParts = "AbsoluteURI: " + uri.AbsoluteUri + Environment.NewLine; uriParts += "Scheme: " + uri.Scheme + Environment.NewLine; uriParts += "UserInfo: " + uri.UserInfo + Environment.NewLine; uriParts += "Host: " + uri.Host + Environment.NewLine; uriParts += "Port: " + uri.Port + Environment.NewLine; uriParts += "Path: " + uri.LocalPath + Environment.NewLine; uriParts += "QueryString: " + uri.Query + Environment.NewLine; uriParts += "Fragment: " + uri.Fragment; // write out our summary Console.WriteLine(uriParts); } catch (ArgumentNullException e) { // uriString is a null reference (Nothing in Visual Basic). Console.WriteLine("URI string object is a null reference: {0}", e); } catch(UriFormatException e) { Console.WriteLine("URI formatting error: {0}",e); } }
The code uses the Uri class to do the heavy lifting. The constructor for the Uri class can throw two types of exceptions: an ArgumentNullException and an UriFormatException. The ArgumentNullException is thrown when the uri argument passed is null. The UriFormatException is thrown when the uri argument passed is of an incorrect or indeterminate format. Here are the error conditions that can throw a UriFormatException:
System.Net.Uri provides methods to compare URIs, parse URIs, and combine URIs. It is all you should ever need for URI manipulation and is used by other classes in the framework when a URI is called for. The syntax for the pieces of a URI is this:
[scheme]://[user]:[password]@[host/authority]:[port]/[path];[params]?[query string]#[fragment]
If we passed the following URI to ParseURI:
http://user:password@localhost:8080/www.funcode.org/home.htm?item=1234#stuff
it would display the following items:
AbsoluteURI: http://user:password@localhost:8080/www.funcode.org/home.htm?item=1234#stuff Scheme: http UserInfo: user:password Host: localhost Port: 8080 Path: /www.funcode.org/home.htm QueryString: ?item=1234 Fragment: #stuff
Popularity: 2% [?]
One Response
Steve Wilkinson
March 29th, 2007 at 3:01 am
1Just what I was looking for. Thanks!
RSS feed for comments on this post · TrackBack URI
Leave a reply