【发布时间】:2015-06-24 08:36:55
【问题描述】:
我正在使用 HttpWebRequest 类在 Xamarin 中开发基于 API 的应用程序。我必须向 URL 发送请求
http://example.com/APIRequest/Request?Parts=33333|N|2014|ABCD
但是当我在 Fiddler 中看到这个请求时,它会显示类似的 URL
http://example.com/APIRequest/Request?Parts=33333%7CN%7C2014%7CABCD
现在的问题是服务器无法理解这个编码的 URL 及其返回的错误,这超出了我的控制范围。
我之前使用的 .NET2.0 C# 应用程序
Uri url = new Uri(rawurl, true);
但第二个参数已在 Xamarin 上可用的 .NET 4.0 MonoTouch 中被弃用,因此它会给出错误或根本不做任何事情。
我已经尝试了所有可能的方法,例如 UrlDecode、htmldecode、双重解码甚至 Java UrlDecode,但没有任何效果,并且总是在 Fiddler 中显示编码的 URL。
请建议如何克服这个问题或任何替代 new Uri(url-string, true) 旧函数的方法。
更新:
花了几个小时后,我可能找到了罪魁祸首。问题是
当我使用“new Uri(url, true)”时,它会发送包含 | (管道)到 WebRequest.Create 但如果我删除“true”,它会发送编码的 URL,这会产生结果但不幸的是服务器不理解,所以我得到错误。
Uri ourUri = new Uri(url, true);
myHttpWebResponse1 = (System.Net.HttpWebResponse)request.GetResponse();
但如果我使用 |,request.GetResponse() 停止工作而不抛出任何异常并且进程挂起可能是一个错误(管道)在 URL 中。
任何可能的解决方案?
我的完整功能如下(用硬编码的 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();
}
是的,这段代码在 .NET 2.0 Windows 应用程序中完美运行,但在 Xamarin Mono-Touch 应用程序中却不能。
【问题讨论】:
标签: c# xamarin xamarin.ios httpwebrequest