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).
Asked
Active
Viewed 5,129 times
2
-
this may be helpful: http://stackoverflow.com/questions/14698879/http-post-for-windows-phone-8 – anderZubi May 22 '13 at 15:57
4 Answers
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
-
How can we POST id="xyz" and password="123" as a parameters while calling Restful WebServices? Please help me – Rahul Saksule Jul 23 '13 at 13:40
-
WebClient is not a valid class in the Windows Phone 8.1 framework. – Scott Nimrod Apr 09 '14 at 01:53
-
@RahulSaksule, look at http://stackoverflow.com/a/5401597/1948785 to pass parameters – eeadev Nov 18 '14 at 10:10
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)

stivoberlin
- 21
- 2
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

user2862037
- 9
- 1
-
This does not provide a detailed explanation how to perform a POST operation for Windows Phone 8. – Scott Nimrod Apr 09 '14 at 01:52