0

Windows Phone 8 -Post Json 'in request body' give an exception but, it is working on 'windows Console app'. Why could it be?

Exception: AsyncWaitHandle '((System.Net.Browser.OHWRAsyncResult)asynchronousResult).AsyncWaitHandle' threw an exception of type 'System.NotSupportedException' System.Threading.WaitHandle {System.NotSupportedException}

My Code is hear.

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

Thanks!

edayildiz
  • 11
  • 3

2 Answers2

1

I figured out the problem with a real device. The problem is the 'https' connections. Windows Phone Emulator gives exception with 'https' links for all request methods.

edayildiz
  • 11
  • 3
0

NotSupportedException means the Windows Phone SDK does not implement that functionality. The exception message will give you more information about what specifically isn't implemented.

My recommendation in any case would be to use System.Net.Http library which reduces this code to about three lines.

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

pantaloons
  • 932
  • 6
  • 5
  • Thanks for your advice. I tried it, but something goes wrong when i add content type header to HttpRequestMessage object. request.Headers.Add("Authorization", "Basic " + encodedAuto);//it is ok request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");// something goes wrong. – edayildiz May 28 '13 at 11:23
  • What goes wrong. Do you get an exception, does the post fail? – pantaloons May 28 '13 at 18:50
  • Request did not take the content-type header, so when debuger step over that row (request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");), request failed without any exception. – edayildiz May 31 '13 at 14:14