I'm trying to write some very simple code in C# that makes one HTTPS post request and returns the result, and I'm a bit confused on which library to use. Since my code only needs to run one HTTPS request, I do not need any async and I feel like it would be simpler to go without it.
It seems like System.Net.Http.HttpClient was not built for non-async uses, whereas System.Net.WebRequest has warnings all over the documentation about how it is deprecated and I should use HttpClient instead. The vast majority of tutorials and StackOverflow questions for WebRequest end up being 5+ or even 10+ years old.
Can someone point me to a good tutorial or library for my use case in the year 2021?
Definitely use HttpClient for this. It has a synchronous Send method which you can use
var client = new HttpClient();
var webRequest = new HttpRequestMessage(HttpMethod.Post, "http://your-api.com")
{
Content = new StringContent("{ 'some': 'value' }", Encoding.UTF8, "application/json")
};
var response = client.Send(webRequest);
using var reader = new StreamReader(response.Content.ReadAsStream());
return reader.ReadToEnd();
}