There is a similar question but my questions seems more general
I've went through a few git repos and noticed Request/Response wrappers over HttpRequestMessage like CryptoExchange.Net.Requests.Request
/// <summary>
/// Request object, wrapper for HttpRequestMessage
/// </summary>
public class Request : IRequest
{
private readonly HttpRequestMessage request;
private readonly HttpClient httpClient;
/// <summary>
/// Create request object for web request
/// </summary>
/// <param name="request"></param>
/// <param name="client"></param>
/// <param name="requestId"></param>
public Request(HttpRequestMessage request, HttpClient client, int requestId)
{
httpClient = client;
this.request = request;
RequestId = requestId;
}
/// <inheritdoc />
public string? Content { get; private set; }
/// <inheritdoc />
public string Accept
{
set => request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(value));
}
/// <inheritdoc />
public HttpMethod Method
{
get => request.Method;
set => request.Method = value;
}
/// <inheritdoc />
public Uri Uri => request.RequestUri;
/// <inheritdoc />
public int RequestId { get; }
/// <inheritdoc />
public void SetContent(string data, string contentType)
{
Content = data;
request.Content = new StringContent(data, Encoding.UTF8, contentType);
}
/// <inheritdoc />
public void AddHeader(string key, string value)
{
request.Headers.Add(key, value);
}
/// <inheritdoc />
public Dictionary<string, IEnumerable<string>> GetHeaders()
{
return request.Headers.ToDictionary(h => h.Key, h => h.Value);
}
/// <inheritdoc />
public void SetContent(byte[] data)
{
request.Content = new ByteArrayContent(data);
}
/// <inheritdoc />
public async Task<IResponse> GetResponseAsync(CancellationToken cancellationToken)
{
return new Response(await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false));
}
}
Previously to send request and get response WebRequest/WebResponse types were used, with their implementations for Http: HttpWebRequest/HttpWebResponse.
I assume HttpRequestMessage was introduced with .NET Core, so it's kind of better abstraction and platform agnostic implementation
Should we now use the newest approach with HttpRequestMessage and think HttpWebRequest is obsolete (but it is not marked as obsolete yet)
HttpWebRequest(or in general anything derived fromWebRequest) has been widely considered obsolete ever sinceHttpClientwas introduced (more than a decade now).But it was only enforced in .NET Core via attributes like,
https://github.com/dotnet/runtime/blob/e18a77feff92fe6cdf8b891aa3b44fb1acb972d1/src/libraries/System.Net.Requests/ref/System.Net.Requests.cs#L180