10

I'm trying to figure out how to use HttpClient to POST some simple parameters.

  • Email
  • Password

I've been doing this with RestSharp, but I'm trying to migrate off that.

How can I do this with HttpClient, please?

I have the following RestSharp code

var restRequest = new RestRequest("account/authenticate", Method.POST);
restRequest.AddParameter("Email", email);
restRequest.AddParameter("Password", password);

How can I convert that to use the (Microsoft.Net.Http) HttpClient class, instead?

Take note: I'm doing a POST

Also, this is with the PCL assembly.

Lastly, can I add in a custom header. Say: "ILikeTurtles", "true".

Charles
  • 50,943
  • 13
  • 104
  • 142
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • 2
    Both of your questions have been answered before, see [.NET HttpClient. How to POST string value?](http://stackoverflow.com/questions/15176538/net-httpclient-how-to-post-string-value) and [Adding Http Headers to HttpClient (ASP.NET Web API)](http://stackoverflow.com/questions/12022965/adding-http-headers-to-httpclient-asp-net-web-api). Try to use the search. – CodeCaster Feb 16 '14 at 01:13
  • 1
    ... hold on a second here. Are there really **three** `HttpClient` classes now? `System.Net.Http.HttpClient`, `Microsoft.Net.Http.HttpClient` and `Windows.Web.Http.HttpClient`? Really, Microsoft? Really? – Charles Feb 16 '14 at 05:27
  • That's a really good question. I've only read about `Microsoft.Net.HttpClient` .. there really are -three- ?? – Pure.Krome Feb 16 '14 at 22:34
  • 1
    @Charles There are two. System.Net.Http.HttpClient which is in the nuget package Microsoft.Net.Http and Windows.Web.HttpClient which is a native implementation. – Darrel Miller Feb 17 '14 at 01:32
  • @Pure.Krome may i know why you decided to go with Microsoft.Net.HttpClient instead of RestSharp..The latters syntax seems to be more readable – Shaiju Janardhanan May 26 '16 at 14:56
  • @ShaijuJanardhanan having one less dependency. – Pure.Krome May 30 '16 at 03:21

3 Answers3

12

This should do it

var httpClient = new HttpClient();

httpClient.DefaultRequestHeaders.Add("ILikeTurtles", "true");

var parameters = new Dictionary<string, string>();
parameters["Email"] = "myemail";
parameters["Password"] = "password";

var result = await httpClient.PostAsync("http://www.example.com/", new FormUrlEncodedContent(parameters));
daanl
  • 44
  • 2
  • 14
3

If you're not opposed to using a library per se, as long as it's HttpClient under the hood, Flurl is another alternative. [disclaimer: I'm the author]

This scenario would look like this:

var result = await "http://www.example.com"
    .AppendPathSegment("account/authenticate")
    .WithHeader("ILikeTurtles", "true")
    .PostUrlEncodedAsync(new { Email = email, Password = password });
Todd Menier
  • 37,557
  • 17
  • 150
  • 173
0

This code isn't using HttpClient but it's using the System.Net.WebClient class, i guess it does the same thing though.

private static void Main(string[] args)
    {
        string uri = "http://www.example.com/";
        string email = "email@example.com";
        string password = "secret123";

        var client = new WebClient();

        // Adding custom headers
        client.Headers.Add("ILikeTurtles", "true");

        // Adding values to the querystring
        var query = HttpUtility.ParseQueryString(string.Empty);
        query["email"] = email;
        query["password"] = password;
        string queryString = query.ToString();

        // Uploadstring does a POST request to the specified server
        client.UploadString(uri, queryString);
    }
Wessel T.
  • 2,280
  • 2
  • 20
  • 29