.NET HttpClient. How to POST string value?

asp.net-web-api, c#, dotnet-httpclient

Solution

In ASP.NET 4.5 (for 4.0, you need to install the `Microsoft.AspNet.WebApi.Client` NuGet package):

using System;
using System.Collections.Generic;
using System.Net.Http;

class Program
{
    static void Main(string[] args)
    {
        Task.Run(() => MainAsync());
        Console.ReadLine();
    }

    static async Task MainAsync()
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:6740");
        var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("", "login")
        });
        var result = await client.PostAsync("/api/Membership/exists", content);
        string resultContent = await result.Content.ReadAsStringAsync();
        Console.WriteLine(resultContent);
    }
}

Problem

How can I create using C# and HttpClient the following POST request: I need such a request for my WEB API service: ``` [ActionName("exist")] [HttpPost] public bool CheckIfUserExist([FromBody] string login) { return _membershipProvider.CheckIfExist(login); } ```

Original source

Related problems