Onelio Onelio - 7 months ago 158
C# Question

Make a POST request with HttpClient in UWP(W10)

I'm using the Universal Windows Plataform for making a App and I want to ask how to post data like (id=1) to the server with httpclient.
I know that there is some tutorials on internet, but many of them are for Windows 8.1 and don't work in 10.


If you want to handle HTTP request in UWP, you need a webservice to support it. If you make a web API project in your solution, then you can use the actions inside controllers to handle the POST request.

More reading about web API: click here

First you need to install the web API .net Client from NuGet to handle HTTP request from web API.

in your UWP project, you can then make a Data transfer class with the following method:

const string ServerUrl = ""; //specify your server url

 public void ClientHeaderInfo(HttpClient client)
        client.BaseAddress = new Uri(ServerUrl);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

public virtual async Task Post(int id,string url)
        HttpClientHandler handler = new HttpClientHandler { UseDefaultCredentials = true };
        using (var client = new HttpClient(handler))
                await client.PostAsJsonAsync(url, id);

            catch (Exception ex)
                await new MessageDialog(ex.Message).ShowAsync();


Now you can call the Post method where ever you need it, with the url routing to the desired controller in the web API, and the id you need to POST.