【问题标题】:Http Post Get Response Error for Windows Phone 8Windows Phone 8 的 Http Post 获取响应错误
【发布时间】:2013-02-05 08:30:45
【问题描述】:

我正在从我的原始帖子 (Http Post for Windows Phone 8) 重新发布第二个问题,因为我的主要问题已经得到解答。

这是我在@Hunter McMillen 的帮助下更新的代码。我现在正试图从服务器获取 responseCallback。问题是第二个 using 语句中的 GetResponseCallback => (HttpWebResponse)httpWebRequest.EndGetResponse(GetResponseCallback) 行,它正在显示

An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll but was not handled in user code

If there is a handler for this exception, the program may be safely continued.

此错误发生在我使用第一个示例之前。有谁知道如何解决这个问题?

  private static async void HttpPostData(){
            string url = "http://www.mytunnel.com/api/purchases";
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.ContentType = "text/plain";
            //httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.AllowWriteStreamBuffering = true;
            httpWebRequest.Method = "POST";
            //httpWebRequest.ContentLength = jsonAsBytes.Length;

        try{
            using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream, httpWebRequest.EndGetRequestStream, null))
            {
                byte[] jsonAsBytes = Encoding.UTF8.GetBytes("{ \"data\" : \"json\" }");
                await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
            }
        }
        catch (Exception e) { Debug.WriteLine(e.Message); }

        httpWebRequest.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), httpWebRequest);
    }

    private static void ReadWebRequestCallback(IAsyncResult callbackResult)
    { 
        HttpWebRequest myRequest = callbackResult.AsyncState as HttpWebRequest;

        try
        {
            HttpWebResponse response = myRequest.EndGetResponse(callbackResult) as HttpWebResponse;
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                String s = sr.ReadToEnd();
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(s); });
            }
        }
        catch (WebException webExcp)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(webExcp.ToString()); });
        }
    }

【问题讨论】:

    标签: c# sdk http-post windows-phone


    【解决方案1】:

    我最终改用了 WebClient。代码更简洁,我从原始帖子中提供的示例链接中阅读了底部的 cmets。 Microsoft 的示例表单实际上是从 stackoverflow 帖子中复制的。它不起作用(不要相信 MS 文档)。

    这是我的代码和教程链接 (http://www.daveamenta.com/2008-05/c-webclient-usage/),供遇到相同问题的每个人使用。如果对您有帮助,请为我竖起大拇指。

      private void httpPostData()
            {
                WebClient webClient = new WebClient();
                webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
                var uri = new Uri("http://www.sample.com/api/purchases", UriKind.Absolute);
                webClient.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();
                webClient.AllowWriteStreamBuffering = true;
                webClient.Encoding = System.Text.Encoding.UTF8;
                webClient.UploadStringAsync(uri, "POST", postData.ToString());
                webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(postComplete);
                System.Threading.Thread.Sleep(200);
            }
    
    
    
    
      private void postComplete(object sender, UploadStringCompletedEventArgs e)
        {
            reset.Set();
            Response result = JsonConvert.DeserializeObject<Response>(e.Result);
            if (result.success == true)
            {
                DispatchCommandResult(new PluginResult(PluginResult.Status.OK, package_id));
            }
        }
    

    【讨论】:

    • WebClient 在 Windows Phone 8.1 中似乎不是一个有效的类。
    【解决方案2】:

    将方法更改为"GET",因为我相信您没有将任何数据发布到表单

    【讨论】:

    • 我尝试在调用第二个方法之前将 httpWebRequest.method = "GET" 放置,但它让我在调试器中断。
    • 也取出内容类型
    • 错误消息“由于 HttpWebRequest 对象的状态,无法设置方法。”
    猜你喜欢
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    相关资源
    最近更新 更多