C# HttpClient POST request

C# HttpClient POST request

 

The type of the body of the request is indicated by the Content-Type header. The HTTP POST method sends data to the server.

 

Add the Newtonsoft.Json package to process JSON data.

 

using System;
using System.Text;
using System.Net.Http;
using Newtonsoft.Json;

var website= new web("Using c sharp", ".com");

var json = JsonConvert.SerializeObject(website);
var data = new StringContent(json, Encoding.UTF8, "application/json");

var url = "https://usingcsharp.com";
using var client = new HttpClient();

var response = await client.PostAsync(url, data);

string results = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(results);

2021-10-03