
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Escaping and Unescaping Data for the Web
|
815
// Output from above code is
//
//Original Uri string:
http://user:password@localhost:8080/www.abc.com/home
//page.htm?item=1233;html=<h1>Heading</h1>#stuff
//
//Escaped Uri string:
//http://user:password@localhost:8080/www.abc.com/home%20page.
//htm?item=1233;
//html=%3Ch1%3EHeading%3C/h1%3E#stuff
In case you are wondering why escaping a Uri has its own method (EscapeUriString),
take a look at what the escaped Uri looks like if you use Uri.EscapeDataString and
Uri.UnescapeDataString on it:
// Why not just use EscapeDataString to escape a Uri? It's not picky enough...
string escapedUriData = Uri.EscapeDataString(uriString);
Console.WriteLine("Escaped Uri data: {0}",escapedUriData);
Console.WriteLine( );
Console.WriteLine(Uri.UnescapeDataString(escapedUriString));
// Output from above code is
//
//
//Escaped Uri data:
//http%3A%2F%2Fuser%3Apassword%40localhost%3A8080%2Fwww.abc.
//com%2Fhome%20page.htm
//%3Fitem%3D1233%3Bhtml%3D%3Ch1%3EHeading%3C%2Fh1%3E%23stuff
//
//http://user:password@localhost:8080/www.abc.com/home
//page.htm?item=1233;html=<h1>Heading</h1>#stuff
Notice that the :, /, :, @, and ? characters get escaped when they shouldn’t, which is
why you use the
EscapeUriString method for Uris.
Discussion
EscapeUriString assumes that there are no escape ...