0

Windows Phone 8 -Post Json 'in request body'给出了一个例外,但它正在处理'windows Console app'。为什么会这样?

异常:AsyncWaitHandle '((System.Net.Browser.OHWRAsyncResult)asynchronousResult).AsyncWaitHandle' 引发了类型为 'System.NotSupportedException' System.Threading.WaitHandle {System.NotSupportedException} 的异常

我的代码是听到的。

 private void Button_Click_1(object sender, RoutedEventArgs e)
              {

                  byte[] encodedPassword = System.Text.Encoding.UTF8.GetBytes("Key" + ":" + "Value");
                  string encodedAuto = System.Convert.ToBase64String(encodedPassword);

                  // Create a new HttpWebRequest object.
                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://uri");



                  // Set the Method property to 'POST' to post data to the URI.
                  request.Method = "POST";

                  request.ContentType = "application/json; charset=utf-8";
                  request.Accept = "application/json";
                  request.Headers["Authorization"] = "Basic " + encodedAuto;
                  request.AllowAutoRedirect = true;
                  request.AllowWriteStreamBuffering = true;


                  // start the asynchronous operation
                  request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

             }

private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
            {
                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

  User user = new User();
                   user.password = password;
          user.username = username;

                // End the operation
       Stream postStream = request.EndGetRequestStream(asynchronousResult)

              /*  String input = "{" +
                        "\"username\" : " + "\"" + username + "\"" + "," +
                        " \"password\" : " + "\"" + password + "\"" +
                        "}";  */
                var input = JsonConvert.SerializeObject(user);

                // Convert the string into a byte array. 
                byte[] byteArray = Encoding.UTF8.GetBytes(input);

                // Write to the request stream.
                postStream.Write(byteArray, 0, byteArray.Length);
                postStream.Flush();
                postStream.Close();


                // Start the asynchronous operation to get the response
                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();
                allDone.Set();
            }

谢谢!

4

2 回答 2

1

我发现了真实设备的问题。问题是“https”连接。Windows Phone Emulator为所有请求方法提供“https”链接例外。

于 2013-06-11T08:07:59.957 回答
0

NotSupportedException 表示 Windows Phone SDK 没有实现该功能。异常消息将为您提供有关未实施的具体内容的更多信息。

无论如何,我的建议是使用 System.Net.Http 库,它将代码减少到大约三行。

http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone.aspx

于 2013-05-22T21:18:12.123 回答