2013-05-22 31 views
0

的Windows Phone 8 - 後的Json 給出一個例外,但是,它正在「Windows控制檯應用程序」。爲什麼會這樣?的Windows Phone 8 - 後的Json「在請求體」給'在要求身體異常

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

謝謝!

回答

1

我想通了這個問題與真實設備。問題是'https'連接。 Windows Phone模擬器'https'鏈接提供所有請求方法的例外。

+0

比我們如何在沒有讀取設備的情況下測試我們的代碼? –

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

+0

感謝您的建議。我試過了,但是當我向HttpRequestMessage對象添加內容類型頭時出了點問題。 request.Headers.Add(「Authorization」,「Basic」+ encodedAuto); //很好 request.Content.Headers.ContentType = new MediaTypeHeaderValue(「application/json」); //出錯了。 – edayildiz

+0

出了什麼問題。你是否遇到異常,帖子是否失敗? – pantaloons

+0

請求未採用內容類型標題,所以當debuger跨過該行(request.Content.Headers.ContentType = new MediaTypeHeaderValue(「application/json」);)時,請求失敗,沒有任何異常。 – edayildiz