Since WebClient is deprecated in .NET 6, I want to convert the following code using WebClient with an equivalent code using HttpClient for calling a REST Web API:
using WebClient client = new();
client.Encoding = Encoding.UTF8;
client.Headers.Set(HttpRequestHeader.ContentType, "application/json");
client.Headers.Add("user_key", tokens[0]);
client.Headers.Add("Session_key", tokens[1]);
string json = JsonSerializer.Serialize(sms);
string serverResponse = client.UploadString(_baseUrl + "sms", "POST", json);
For now, I implemented the following solution:
HttpClient httpClient = _httpClientFactory.CreateClient();
HttpRequestMessage request = new(HttpMethod.Post, _baseUrl + "sms");
request.Headers.Add("user_key", tokens[0]);
request.Headers.Add("Session_key", tokens[1]);
string json = JsonSerializer.Serialize(sms);
request.Content = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json);
using HttpResponseMessage response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
I think that the above solution is the cleanest and most efficient way to replace the original code. Can an EXPERT confirm this?
.Net 6 supports synchronous Send()
Here's a simple GET:
using var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.example.com/api/v1/get");
var response = httpClient.Send(request);
using var reader = new StreamReader(response.Content.ReadAsStream());
var responseBody = reader.ReadToEnd();
Here's a POST:
var handler = new HttpClientHandler {
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
var client = new HttpClient(handler) {
BaseAddress = new Uri(_baseurl)
};
var webRequest = new HttpRequestMessage(HttpMethod.Post, "sms") {
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
webRequest.Headers.Add("user_key", tokens[0]);
webRequest.Headers.Add("Session_key", tokens[1]);
var response = client.Send(webRequest);
var reader = new StreamReader(response.Content.ReadAsStream());
var responseBody = reader.ReadToEnd();
Seems simple enough:
HttpClient client = new()
{
BaseAddress = new Uri(_baseUrl)
};
using HttpRequestMessage request = new(HttpMethod.Post, "sms");
request.Headers.Add("user_key", tokens[0]);
request.Headers.Add("Session_key", tokens[1]);
string json = JsonSerializer.Serialize(sms);
request.Content = new StringContent(json, Encoding.UTF8)
{
Headers =
{
ContentType = new("application/json")
}
};
using HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string serverResponse = await response.Content.ReadAsStringAsync();
However, you'll probably want to use the IHttpClientFactory to create your HttpClient instances, since that provides several benefits - particularly pooling the underlying message handler, to avoid performance problems.
You might also want to consider using the PostAsJsonAsync extension method, which could simplify this code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With