This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Network and Internet
.NET 1.1+

The System.Uri Class

Software that uses networked resources, web sites or remote files often makes use of Uniform Resource Identifiers to locate those resources. Although these can be held in simple strings, using the System.Uri class provides many additional benefits.

Static Methods

The final Uri members that we'll look at here are some of the static methods. These provide functionality for validating URIs and encoding special characters for use in URIs.

Validating the Scheme and Host Name

When you initialise a new Uri object the URI that you provide to the constructor is validated. If the URI is incorrectly structures, an exception is thrown. If you wish, you can also perform some validation on parts of URIs, held in strings, before trying to construct a new object. The Uri class provides two static members for this purpose. To check that the scheme name is valid you can call the CheckSchemeName method. This returns true if the scheme name contains no invalid characters. CheckHostName can be used to check that a host name is valid. This returns a UriHostNameType value that describes the type of host detected.

Console.WriteLine(Uri.CheckSchemeName("not valid"));




Hexadecimal Encoding

There are limitations on the characters that can be used in URIs, due to the use of certain symbols to denote the various parts of URIs. When you need to include those characters you can encode them using a hexadecimal number, prefixed with a percent symbol (%). For example, when using a URI to encode the query part of a web page address you might use ampersand characters to separate key / value pairs. If you want to include an ampersand in one of the values, you can encode it as "%26". For example, "?query=cheech%26chong".

Handily the Uri class includes methods that encode individual characters into the hexadecimal format and decode hexadecimal items to return the original character. HexEscape accepts a character value and returns the encoded string. HexUnescape requires two parameters. The first is a string that contains a hexadecimal encoded character to decode. The second parameter is an integer, passed by reference, that specifies the index of the percent symbol that begins the encoded section. It returns a decoded character.

In the sample below a character is encoded and the result is decoded. The results for both operations are outputted to the console.

string encoded;
int value = 0;
char character;

encoded = Uri.HexEscape('&');
character = Uri.HexUnescape(encoded, ref value);

Console.WriteLine("{0} = {1}", character, encoded);


& = %26


It can be useful to check whether a specific point in a string represents an encoded character before attempting a decoding operation. You can do this with the IsHexEncoding method, which uses the same parameters as HexDecode, except the index position is passed by value. The method returns true if the text at the given position is decodable and false otherwise.

Console.WriteLine(Uri.IsHexEncoding("%26", 0));
Console.WriteLine(Uri.IsHexEncoding("Value", 0));




Working with Hexadecimal Digits

The last two static methods deal with hexadecimal digits directly, with no direct link to URIs. However, they can still be useful and can provide good code readability. The IsHexDigit method checks a character to see if it contains a hexadecimal digit, either 0-9 or A-F. The FromHex method converts a single hexadecimal digit, provided as a character, to its integer equivalent.




20 June 2012