user2717575
user2717575

Reputation: 409

Why does System.Uri break my url?

I have the following C# code Uri uri = new Uri("http://localhost/query?param=%E2%80%AE"); and uri interprets it like http://localhost/query?param= instead of http://localhost/query?param=%E2%80%AE. As a result http web server gets http://localhost/query?param= (without value of this parameter). Why does it break my url and how can I create HttpWebRequest correctly using my http://localhost/query?param=%E2%80%AE?

P.S. I have got the %E2%80%AE using System.Uri.EscapeDataString(Convert.ToString((char)8238)).

Upvotes: 1

Views: 258

Answers (2)

Yury Schkatula
Yury Schkatula

Reputation: 5369

This is kind of mis-using of the methods exposed.

  • ToString() method, as derived from very first ancestor, Object, is a way to provide some printable form of the given object instance. It's not meant to be instance identity nor full serialization of all internal fields.
  • Uri class is designed as a URI parser/composer/dissector so it has various methods to merge or split given URI strings to get server name, local path etc. So if you need URI as a whole thing you have to use "Uri.AbsolutePath Property" http://msdn.microsoft.com/en-us/library/system.uri.absolutepath(v=vs.110).aspx

Upvotes: 0

Jeff Mercado
Jeff Mercado

Reputation: 134841

ToString() will try to render the uri as a string. i.e., it will unescape escaped characters. However the escaped sequence %E2%80%AE is not printable.

Use the AbsoluteUri instead.

var uriStr = uri.AbsoluteUri; // "http://localhost/query?param=%E2%80%AE"

Upvotes: 1

Related Questions