DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 13.3 Parsing a URI

Problem

You need to split a URI (Uniform Resource Identifier) into its constituent parts.

Solution

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);
    }
}

Discussion

The Solution 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:

  • An empty URI was passed in.

  • The scheme specified in the passed in URI is invalid.

  • The URI passed in contains too many slashes.

  • The password specified in the passed in URI is invalid.

  • The hostname specified in the passed in URI is invalid.

  • The filename specified in the passed in URI is invalid.

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.abc.com/home.htm?item=1233#stuff

it would display the following items:

AbsoluteURI: 
http://user:password@localhost:8080/www.abc.com/home.htm?item=1233#stuff
Scheme: http
UserInfo: user:password
Host: localhost
Port: 8080
Path: /www.abc.com/home.htm
QueryString: ?item=1233
Fragment: #stuff

See Also

See the "Uri Class," "ArgumentNullException Class," and "UriFormatException Class" topics in the MSDN documentation.

    [ Team LiB ] Previous Section Next Section