Wrong uri encoding

  • Thread starter Thread starter T. Liebethal
  • Start date Start date
T

T. Liebethal

T. Liebethal said:
Hello,

I have a simple but strange problem. I'm creating an Uri with the
following code:

Uri myUri = new Uri(url);

the url string is:

http://www.dummy.com/test/Bestelllösung.exe_2_1_502_911.update"

and the Uri object encodes this to:

http://www.dummy.com/test/Bestelllösung.exe_2_1_502_911.update

instead of

http://www.dummy.com/test/Bestelll%F6sung.exe_2_1_502_911.update

so the request fails with 404 Error.

Please help. I'm using .net 1.1 with the newest updates.

That's not wrong. It's just that you expected the underlying character
encoding to be something like ISO-8859-1 or Windows-1252 (where 'ö' is
is 0xF6), but Uri uses UTF-8 (where ö is 0xC3 0xB6). If you cannot use
UTF-8 you have to revert to other means like System.Web.HttpUtility to
encode your URI:

string baseUriString = "http://www.dummy.com/test/";
string relativeUriString = "Bestelllösung.exe_2_1_502_911.update";
Encoding enc = Encoding.GetEncoding("ISO-8859-1");
string encodedUriString = HttpUtility.UrlEncode(relativeUriString, enc);
Uri baseUri = new Uri(baseUriString);
Uri uri = new Uri(baseUri, encodedUriString);
Console.WriteLine(uri.AbsoluteUri);

That's quite a hassle, but neither Uri nor UriBuilder have a character
encoding property as of .NET 1.1 :-(

Cheers,
 
Back
Top