2013-05-22 34 views
2

任何人都可以點我怎麼POST JSON的工作示例,以一個RESTful API(例如網頁API)使用的Windows Phone 8?我有一個GET的工作示例,但似乎無法找到POST的任何工作示例。所有的POST的例子,我已經找到了C#不上的Windows Phone 8(由於剝離下來的.NET framework)工作。如何發佈到REST風格的API隨着Windows Phone 8

+0

這可能是有益的: 我們http://stackoverflow.com/questions/14698879/http-post-for-windows-phone-8 – anderZubi

回答

3

好吧,我終於能拿出一個可行的解決方案,所以我想將它張貼回來的完整性。但是,如果有人知道在Windows Phone 8中執行此操作的更好方法,我很樂意看到它!

public void SendPost(Uri uri, string json) 
{ 
    var webClient = new WebClient(); 

    webClient.Headers[HttpRequestHeader.ContentType] = "application/json"; 
    webClient.UploadStringCompleted += this.sendPostCompleted; 
    webClient.UploadStringAsync(uri, "POST", json); 
} 

private void sendPostCompleted(object sender, UploadStringCompletedEventArgs e) 
{ 
    // Handle result 
    Console.WriteLine("HTTP POST Result: {0}", e.Result); 
} 
+0

如何帖子ID =「XYZ」和密碼=「123」作爲參數同時調用Restful WebServices?請幫我 –

+0

Web客戶端是不是在Windows Phone 8.1框架的類無效。 –

+0

@RahulSaksule,看http://stackoverflow.com/a/5401597/1948785傳遞參數 – eeadev

0

要做到這一點是使用Restsharp的最佳方式:

 
     Dim client As New RestSharp.RestClient("https://stuff.com/api/") 
      Dim req As New RestSharp.RestRequest("dothings", Method.POST) 
      req.RequestFormat = DataFormat.Json 
      req.AddBody(New reqAuth With {.param1 = "stuff1", .param2= "stuff2"}) 
      client.ExecuteAsync(req, Sub(res) 
             Console.WriteLine(res.Content) 
            End Sub) 
-1
public async void makeRequest(String resourceUri) 
{ 
    HttpClient httpClient = new HttpClient(); 
    try 
    { 
      HttpResponseMessage response = await httpClient.GetAsync(resourceUri); 
    } 
    catch (HttpRequestException hre) 
    { 
      Console.Write(hre.Message); 
    } 
    catch (TaskCanceledException) 
    { 
    } 
} 

記得添加的NuGet包:HttpClient的

+0

這並沒有提供詳細的解釋如何爲Windows Phone 8的執行POST操作。 –

0
string email = txtEmailAddress.Text; 
      string password = txtPassword.Text; 
      string confirmPassword = txtConfirmPassword.Text; 
      string dd = txtDD.Text; 
      string mm = txtMM.Text; 
      string yyyy = txtYYYY.Text; 

      UserDetails ud = new UserDetails 
      { 
       DateofBirth = DateTime.Now.ToString(), 
       EmailAddress=email, 
       Password=password, 
       ProfileImage="", 
       UserID="0" 
      }; 

      WebClient wc = new WebClient(); 
      wc.Headers["Content-Type"] = "application/json"; 
      MemoryStream ms = new MemoryStream(); 
      DataContractJsonSerializer serializertoUpload = new DataContractJsonSerializer(typeof(UserDetails)); 
      serializertoUpload.WriteObject(ms, ud); 
      string data = Encoding.UTF8.GetString(ms.ToArray(), 0, (int)ms.Length); 
      wc.UploadStringAsync(new Uri("<wcfserviceurl>"), "POST", data); 

這裏的UserDetails類的定義

public class UserDetails 
    { 
     public string UserID { get; set; } 
     public string EmailAddress { get; set; } 
     public string Password { get; set; } 
     public string DateofBirth { get; set; } 
     public string ProfileImage { get; set; } 
    }