2014-05-23 58 views
0

我試圖調用Web服務 它返回一個JSON響應響應。到目前爲止,我的代碼對GET請求工作正常,但現在服務器上的服務都是POST,我不知道該怎麼做!下面是GET請求的代碼:如何發送POST請求中的Windows Phone 8,並獲得其在JSON

private void callSigninWebservice() 
     { 
      setProgressIndicator(true); 

      SystemTray.ProgressIndicator.Text = "Signing in please wait"; 
      try 
      { 
       WebClient webClient = new WebClient(); 
       Uri uri = new Uri(GlobalVariables.URL_USER + 
       GlobalVariables.URL_STUDENT_SIGNIN_MODE_LOGIN + 
       GlobalVariables.URL_EMAIL + tbUsername.Text + 
       GlobalVariables.URL_PASSWORD + tbPassword.Password); 
       webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted); 
       webClient.DownloadStringAsync(uri); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message + "error came here 1"); 
      } 

     } 

爲了響應

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
     { 
      try 
      { 

       JObject parentObj = JObject.Parse(e.Result); 
       String strResult = (String)parentObj[SigninData.JSON_result]; 

       bool bolresult = strResult.Equals(SigninData.JSON_result_success, StringComparison.Ordinal); 

       if (bolresult) 
       { 
        JObject dataObj = (JObject)parentObj[SigninData.JSON_data]; 
        setUserData(dataObj); 
        NavigationService.Navigate(new Uri("/BasePage.xaml", UriKind.RelativeOrAbsolute)); 

       } 
       else 
       { 
        String error = (String)parentObj[SigninData.JSON_data]; 
        MessageBox.Show("Error : " + error); 
       } 
       setProgressIndicator(false); 
      } 
      catch (Exception) 
      { 
       setProgressIndicator(false); 
      } 

     } 
+0

[檢查此](http://www.jayway.com/2012/03/13/httpclient-makes-get-and-post-very-simple/) – vITs

+0

這不是幫助。 @vits –

回答

1

這是做一個POST請求並接收對未來的解析中JSON響應的一個簡單的方法:

internal static async Task<String> GetHttpPostResponse(HttpWebRequest request, string postData) 
    { 
     String received = null; 

     request.Method = "POST"; 
     request.ContentType = "application/x-www-form-urlencoded"; 

     byte[] requestBody = Encoding.UTF8.GetBytes(postData); 

     // ASYNC: using awaitable wrapper to get request stream 
     using (var postStream = await request.GetRequestStreamAsync()) 
     { 
      // Write to the request stream. 
      // ASYNC: writing to the POST stream can be slow 
      await postStream.WriteAsync(requestBody, 0, requestBody.Length); 
     } 

     try 
     { 
      // ASYNC: using awaitable wrapper to get response 
      var response = (HttpWebResponse)await request.GetResponseAsync(); 
      if (response != null) 
      { 
       var reader = new StreamReader(response.GetResponseStream()); 
       // ASYNC: using StreamReader's async method to read to end, in case 
       // the stream i slarge. 
       received = await reader.ReadToEndAsync(); 
      } 
     } 
     catch (WebException we) 
     { 
      var reader = new StreamReader(we.Response.GetResponseStream()); 
      string responseString = reader.ReadToEnd(); 
      Debug.WriteLine(responseString); 
      return responseString; 
     } 

     return received; 
    } 
+0

「System.Net.HttpWebRequest」不包含「GetRequestStreamAsync」的定義,並沒有擴展方法「GetRequestStreamAsync」接受一個類型的第一個參數「System.Net.HttpWebRequest」可以找到(是否缺少using指令或一個程序集引用?)這是我得到的錯誤 –

+1

請從NuGet安裝** Microsoft HTTP Client Library **,這應該可以解決您的問題。 – Meneses

+0

@Meneses,微軟的HTTP客戶端庫似乎是爲wp8.1而不是wp8 – eeadev

相關問題