Calling RESTful endpoints

Updated 24/05/2021

HttpClient

This is the current recomended class for sending HTTP requests and receiving HTTP responses.

1
2
3
4
5
6
7
using (var client = new HttpClient())
{
using (var content = new StringContent(json, Encoding.UTF8, "application/json"))
{
var result = client.PostAsync(endPoint, content).Result;
}
}

WebRequest

“We don’t recommend that you use WebRequest or its derived classes for new development. Instead, use the System.Net.Http.HttpClient class. - docs.microsoft.com”

1
2
3
4
5
6
7
var endPoint = string.Format("{0}/fizzbuzz_event", ApiUrl);
WebRequest request = WebRequest.Create(endPoint);
request.Method = "POST";
request.ContentType = "application/json";

var dataStream = request.GetRequestStream();
dataStream.Write(new UTF8Encoding().GetBytes(json), 0, json.Length);

WebClient

“We don’t recommend that you use the WebClient class for new development. Instead, use the System.Net.Http.HttpClient class. - docs.microsoft.com”