I’ve seen a number of posts that ask about using non-conforming Uris, but none of them have a good answer. I’m writing a Windows Phone control and it needs to request ATOM data using HTTP GET. The URL needs to include the characters %3d (the equal sign) in it. This is non-negotiable as it’s a third-party service over which I have no control. Imagine a URL like the following:
http://host.com?arg1=val1&arg=val2%3d
I’m using WebClient with DownloadStringAsync, but it only accepts Uri objects, not URL in strings. The Uri object automatically unescapes those characters and sends:
http://host.com?arg1=val1&arg=val2=
This fails. There’s a deprecated constructor argument “UserExcaped” that is exactly what I need, but it’s not available. I fear that my only option is to implement my own string download method using a raw socket and minimal HTTP support.
Any other thoughts? Help is very much appreciated!!
UPDATE
string apps = "http://blah.com/api?prev=AAAAAAA%3d&next=BBBBBBB";
var u = new Uri(apps);
string apps2 = u.ToString();
Debug.Assert(apps == apps2); // That %3d will always be converted to an equals sign
I should also point out, that if I try to be clever by encoding the percent as %25 (so it’s “prev=AAAAAA%253d&next=BBBBB”) then I get that exact literal string with %25 still intact! It’s just %3d that it’s aggressive about converting to an equals sign.
The
Uriclass is designed to be like that. MSDN:Have you tried using HttpWebRequest instead? It’s a bit more work than a WebClient, but it lets you pass a string as the destination.
Edit – Another approach could be to create your own
UriParserwhich allows the encoded version. An example of extending theUriParsercan be found here.For others not running Windows Phone, you can use
reflectionto fix this.