MonoTouch 的 HttpWebRequest 问题
我使用 Monotouch(或 iphone)的时间不长,所以我想我的问题与经验有关。
我创建了一个需要通过 json 与 Web 服务通信的应用程序。我使用了在这里找到的移植的 Json.NET 库: https://github.com/chrisntr/Newtonsoft.Json< /a>
首先,我使用 Json.NET 库创建了一个 Windows 应用程序,只是为了快速尝试一下。它工作得很好。然后我在 MonoDevelop 中编写了完全相同的代码,服务器返回一条错误消息,表明它根本无法识别该查询。 请注意,我已经检查了序列化程序是否正确完成其工作 - json 字符串的格式正确。此外,在获得服务器响应之前,两个应用程序的行为方式相同。
JsonSerializer serializer = new JsonSerializer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(the uri of the service);
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";
Query login = new Query(); // the object that will be serialized
login.module = "auth";
login.data.Add("username", username goes here);
login.data.Add("password", password goes here);
using (Stream s = request.GetRequestStream())
{
using (StreamWriter w = new StreamWriter(s))
{
StringWriter sWriter = new StringWriter(new StringBuilder());
serializer.Serialize(sWriter, login);
w.Write("&query="+sWriter.ToString());
}
}
using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
{
var reader = new StreamReader(resp.GetResponseStream());
JsonReader r = new JsonTextReader(reader);
Response login_response = (Response)serializer.Deserialize(reader, typeof(Response));
}
当我第一次编写 Windows 应用程序时,服务器无法识别我的查询,因为内容标头设置为“application/json”,并且因为它要求将查询作为键值对传递(以“query”作为键) )。这就是 w.Write("query = "+sWriter.ToString()); 的原因。在作为调用服务的示例提供的 php 脚本中,此行的设置如下: curl_setopt($ch, CURLOPT_POSTFIELDS, array('query'=>jsonEncode($data)));
然而,由于我修复了内容类型标头并在字符串前面添加了“query=”,因此代码在 Visual Studio 和 Mono Develop 中完美运行。我用 Java 编写后,它甚至可以在我的 Android 手机上运行。然而,在MonoTouch中,服务器总是无法将请求流识别为查询。这可能是什么原因造成的?与其他地方相比,MonoTouch 中的请求流有什么特别的地方吗?再次,我检查了进入流的字符串,它是正确的,并且对于所有测试应用程序都是相同的。
先感谢您。
I have not been working with Monotouch (or the iphone for that matter) for too long, so I guess my problem is experience related.
I created an application that needs to communicate with a web service via json. I used the ported Json.NET library found here : https://github.com/chrisntr/Newtonsoft.Json
First I created a windows application using the Json.NET library, just to quickly try it out. It worked perfectly. Then I wrote the same exact code in MonoDevelop, and the server returns an error message that it cannot recognize the query at all.
Note that I have checked if the serializer does its job properly - the json string is formatted correctly. Moreover, both applications behave in the same manner until getting the response from the server.
JsonSerializer serializer = new JsonSerializer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(the uri of the service);
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";
Query login = new Query(); // the object that will be serialized
login.module = "auth";
login.data.Add("username", username goes here);
login.data.Add("password", password goes here);
using (Stream s = request.GetRequestStream())
{
using (StreamWriter w = new StreamWriter(s))
{
StringWriter sWriter = new StringWriter(new StringBuilder());
serializer.Serialize(sWriter, login);
w.Write("&query="+sWriter.ToString());
}
}
using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
{
var reader = new StreamReader(resp.GetResponseStream());
JsonReader r = new JsonTextReader(reader);
Response login_response = (Response)serializer.Deserialize(reader, typeof(Response));
}
When I first wrote the windows application, the server could not recognize my queries because the Content header was set to "application/json" and because it requires the query to be delivered as a key-value pair (with "query" as the key). That is the reason for the w.Write("query = "+sWriter.ToString()); . In the php script that was provided as an example of calling the service, this line was setup like this:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('query'=>jsonEncode($data)));
However since I fixed the content type headers and added the "query=" in front of the string, the code works perfectly in Visual Studio and Mono Develop. It even ran on my android phone after I wrote it in Java. However, in MonoTouch, the server always fails to recognize the request stream as a query. What could be the cause of this? is there anything special that happens to the request stream in MonoTouch as opposed to everywhere else? Again, I have checked the string that gets into the stream, it is correct and the same for all the test applications.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重新安装 MonoDevelop,问题消失了。
Reinstalled MonoDevelop and issue disappeared.