2

Can anyone point me to a working example of how to POST JSON to a RESTful API (e.g. Web API) using Windows Phone 8? I have a working example for GET but can't seem to find any working examples for POST. All of the POST examples I've found for C# don't work on Windows Phone 8 (due to the stripped down .NET framework).

Jeff Bramwell
  • 990
  • 8
  • 16

4 Answers4

3

Ok, I was finally able to come up with a working solution so I wanted to post it back for completeness. However, if anyone knows of a better way to do this in Windows Phone 8, I'd love to see it!

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);
}
Jeff Bramwell
  • 990
  • 8
  • 16
0

The best way to do that would be to use 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)
    
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);

Here is the definition of UserDetails class

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; }
    }
Kshitij Jhangra
  • 577
  • 7
  • 14
-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)
     {
     }
}

remember to add Nuget packages: httpclient

Jojo
  • 1,875
  • 3
  • 29
  • 29