WP7 HttpWebRequest POST,服务器返回“{ result: ”ok”; }\"在模拟器中,但“错误:NotFound”在设备上

发布于 2024-11-18 18:19:07 字数 2406 浏览 2 评论 0 原文

我正在尝试从我的 Windows Phone 7 应用程序对 Web 服务执行非常基本的 http POST。我知道该网络服务运行良好,因为我正在将它用于其他三个移动平台。

我修改了 http://msdn 中的 C# 示例.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx

    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[] 的方式有关。

有关修复的任何想法吗?也许这是一个已知的错误,我在网上搜索答案时从未遇到过?

I'm trying to do a very basic http POST to a web service from my Windows Phone 7 app. I know the web service works fine because I'm using it for three other mobile platforms.

I've modified the C# example from http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx

    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();
    }

I'm receiving "{ result: 'ok' }" from the server when I run this in the emulator, but "error: NotFound" when I run it on my Samsung Focus. I'm assuming this has to do with the way a string is converted to a byte[] on the phone versus a desktop computer.

Any ideas on a fix? Maybe this is a known error that I never ran across searching the web for an answer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱贱哒 2024-11-25 18:19:07

我终于发现我手机的唯一 ID/设备 ID 中有一个正斜杠。当我在以下位置构建 Web 服务 Uri 时,这导致了问题:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Constants.JSON_URL_PREFIX + Settings.Settings.DeviceID + "/inquiry/new/");

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:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Constants.JSON_URL_PREFIX + Settings.Settings.DeviceID + "/inquiry/new/");

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.

╰◇生如夏花灿烂 2024-11-25 18:19:07

我在应用程序上使用此解决方案,但我的 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";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文