WP7 HttpWebRequest POST,服务器返回“{ result: ”ok”; }\"在模拟器中,但“错误:NotFound”在设备上
我正在尝试从我的 Windows Phone 7 应用程序对 Web 服务执行非常基本的 http POST。我知道该网络服务运行良好,因为我正在将它用于其他三个移动平台。
string boundary = DateTime.Now.Ticks.ToString();
private void POST_TEST(object sender, RoutedEventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Constants.JSON_URL_PREFIX + Settings.Settings.DeviceID + "/inquiry/new/");
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
public void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
StringBuilder postData = new StringBuilder();
postData.Append("--" + boundary + "\r\n");
postData.Append("Content-Disposition: form-data; name=\"body\"\r\n\r\n");
postData.Append("test 123");
postData.Append("\r\n--" + boundary + "\r\n");
byte[] byteArray = Encoding.UTF8.GetBytes(postData.ToString());
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
}
当我运行此命令时,我从服务器收到“{ result: 'ok' }”在模拟器中,但是当我在三星 Focus 上运行它时出现“错误:NotFound”。我假设这与手机上字符串与台式计算机上字符串转换为 byte[] 的方式有关。
有关修复的任何想法吗?也许这是一个已知的错误,我在网上搜索答案时从未遇到过?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于发现我手机的唯一 ID/设备 ID 中有一个正斜杠。当我在以下位置构建 Web 服务 Uri 时,这导致了问题:
Uri 模拟器示例: http:// www.blah.com/abc_123=/inquiry/new/
Uri 设备示例: http://www.blah.com/abc/123=/ quiry/new/
显然,它在模拟器中运行良好,因为模拟器的设备 ID 中没有正斜杠。本质上,我试图发布的 Uri 是不正确的。
有点不起眼的错误...
谢谢 Opena 和 Matt。您的建议给了我尝试的机会,并最终帮助我发现了问题。
I finally figured out that my phone's Unique ID / Device ID has a forward-slash in it. This caused a problem when I constructed the web service Uri at:
Uri emulator example: http://www.blah.com/abc_123=/inquiry/new/
Uri device example: http://www.blah.com/abc/123=/inquiry/new/
Obviously, it worked fine in the emulator because the emulator's device ID doesn't have a forward-slash in it. Essentially, the Uri I was trying to post to was incorrect.
Kind of an obscure bug...
Thanks Opena and Matt. Your suggestions gave me things to try and eventually helped me see the problem.
我在应用程序上使用此解决方案,但我的 postData 看起来像
postData.Append("test=123")
。我不使用边界(我以为边界仅适用于邮件?)我的 request.ContentType 也是
request.ContentType = "application/x-www-form-urlencoded";
I use this solution on an app but my postData looks like
postData.Append("test=123")
. I don't use boundaries (I thought boundaries were only for mails ?)Also my request.ContentType is
request.ContentType = "application/x-www-form-urlencoded";