WebRequest converts URL to Xamarin encoded (MonoTouch)

I am working on an API based application in Xamarin using the HttpWebRequest class. I need to send a request to a URL

http://example.com/APIRequest/Request?Parts=33333|N|2014|ABCD

      

But when I see this request in Fiddler it shows me the url as

http://example.com/APIRequest/Request?Parts=33333%7CN%7C2014%7CABCD

      

Now the problem is that this encoded url is not understood by the server and its returning errors, which is out of control.

Earlier in .NET2.0 C # Application I used

Uri url = new Uri(rawurl, true);

      

But the second parameter is deprecated in .NET 4.0 MonoTouch available on Xamarin, so it gives an error or just does nothing.

I tried all possible ways like UrlDecode, htmldecode, double decode or even Java UrlDecode but nothing worked and always shows the encoded url in Fiddler.

Please suggest how to overcome this issue or any alternative to the new Uri (url-string, true) of the old function.

UPDATE:

After spending hours in hours, I must have found the culprit. The problem is

When I use "new Uri (url, true)" it sends an unescaped url containing | (pipe) to WebRequest.Create, but if I remove "true" it sends an encoded url which gives the result, but unfortunately the server doesn't understand, so I get an error.

Uri ourUri = new Uri(url, true);
myHttpWebResponse1 = (System.Net.HttpWebResponse)request.GetResponse();

      

But it might be an error that request.GetResponse () stops working without throwing any exceptions and process hangs if I use | (pipe) in url.

Any possible solution to this?

My complete function is below (modified with hardcoded url)

public static string getURLCustom(string GETurl, string GETreferal)
{
    GETurl = "http://example.com/?req=111111|wwww|N|2014|asdwer4";
    GETreferal = "";
    Uri ourUri = new Uri(GETurl.Trim(), true);
    HttpWebRequest request = (HttpWebRequest)(WebRequest.Create(ourUri));
    request.Method = "GET";
    request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)";
    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    request.KeepAlive = true;
    request.CookieContainer = loginCookie; //stored after login
    request.ContentType = "application/x-www-form-urlencoded";
    request.Referer = GETreferal;
    request.AllowAutoRedirect = true;
    HttpWebResponse myHttpWebResponse1 = default(HttpWebResponse);
    myHttpWebResponse1 = (System.Net.HttpWebResponse)request.GetResponse();
    StreamReader postreqreader1 = new StreamReader(myHttpWebResponse1.GetResponseStream());
    return postreqreader1.ReadToEnd();
}

      

And yes, this code works fine in .NET 2.0 Windows Application, but not in Xamarin Mono-Touch application.

+3


source to share


1 answer


It seems the server you are connecting to does not support Internationalized Resource Identifier (IRI).

IRI is enabled by default since mono 3.10. mono 3.10 release notes

You can disable it in your client application by following these steps:



FieldInfo iriParsingField = typeof (Uri).GetField ("s_IriParsing",
    BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
if (iriParsingField != null)
    iriParsingField.SetValue (null, false);

      

You can also disable IRI parsing by setting the MONO_URI_IRIPARSING environment variable to false.

+1


source







All Articles