Dofactory.com
Dofactory.com

How to make an HTTP POST web request in C#

Use HttpClient class for issueing post web requests and other types of requests. It is a newer API for working with HTTP and is designed to work well with web APIs, REST-based services, and custom authentication schemes. In .NET Framework, HttpClient relied on WebRequest and WebResponse, but starting with .NET Core 3, it handles HTTP itself.

Below is the code for making a post request.


using System.Net.Http;

private static readonly HttpClient client = new();

var values = new Dictionary<string, string>
{
   { "thing1", "hello" },
   { "thing2", "world" }
};

var content = new FormUrlEncodedContent(values);

var response = await client.PostAsync(
       "http://www.example.com/postable.aspx", content);

var result = await response.Content.ReadAsStringAsync();


Jack Poorte
Jack Poorte
Last updated on Sep 30, 2023



Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.