简体   繁体   中英

Unescape System.Uri in MonoTouch

When developing my initial MonoTouch iOS application I was trying to use System.Uri instances to download protected resources from a private web service but as all instances are always returning unescaped URLs, my requests are failing when having their signature checked.

Example of a good request:

http://example.com/folder%2Ftest?signature=000%3D

Example of a bad request:

http://example.com/folder/test?signature=000=

In order to properly download my resources I need to convert a good URL from a simple string to a good Uri instance. I have just started the process but I am not able to conclude it:

string urlStringVersion = "http://example.com/folder%2Ftest?signature=000%3D";
//
// urlStringVersion == http://example.com/folder%2Ftest?signature=000%3D

Uri urlUriVersion = new Uri (urlStringVersion);
//
// urlUriVersion == http://example.com/folder/test?signature=000=

var fi = typeof (Uri).GetField ("host", BindingFlags.NonPublic | BindingFlags.Instance);
fi.SetValue (urlUriVersion, "example.com");
//
// urlUriVersion.AbsoluteUri is now == http://example.com/folder/test?signature=000%3D

<Another C# command here>

//
// urlUriVersion.AbsoluteUri is now == http://example.com/folder%2Ftest?signature=000%3D

Finally, which command may I be using to replace the Another C# command here in order to have my final urlUriVersion.AbsoluteUri pointing to the same initial urlStringVersion described URL?

I need this conversion working, otherwise I will be forced to make my resources public at my private web service.

I have also tested some alternatives:

  1. From other questions like: GETting a URL with an url-encoded slash but some exceptions are happening:

    System.NullReferenceException : Object reference not set to an instance of an object

  2. Using configuration files but in this case since mobile devices don't technically have an Application Domain they are not available.

  3. Double-encoding the URL by replacing the percent signs with an encoded percent sign (so '%' becomes '%25').

None of my tries solved my problem.

Thanks in advance,

Piva

The Uri constructor you're using takes an unescaped url string, not an escaped url string.

Try unescaping the url string before creating the Uri instance:

string urlStringVersion = "http://example.com/folder%2Ftest?signature=000%3D";
urlStringVersion = Uri.UnescapeDataString (urlStringVersion);

Uri urlUriVersion = new Uri (urlStringVersion);

If you want to convert a Uri to an (un)escaped string, use the GetComponents method:

var unescaped = urlUriVersion.GetComponents (UriComponents.HttpRequestUri, UriFormat.Unescaped);
var escaped = urlUriVersion.GetComponents (UriComponents.HttpRequestUri, UriFormat.UriEscaped);

Do not change the values of private fields , that is bound to break your app one day. You are circumventing every single test for the Uri class (you're using the class in a way it was not designed for, nor tested), and besides the implementation may change at any time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM