// Copyright © WireMock.Net
#if !NET5_0_OR_GREATER
namespace System.Net.Http;
///
/// Extension methods for HttpClient to provide CancellationToken support in frameworks before .NET 5.0.
///
internal static class HttpClientExtensions
{
///
/// Sends a GET request to the specified Uri as an asynchronous operation.
/// This extension is only used in frameworks prior to .NET 5.0 where CancellationToken is not supported.
///
public static Task GetAsync(this HttpClient client, Uri requestUri, CancellationToken _)
{
// In older frameworks, we ignore the cancellation token since it's not supported
return client.GetAsync(requestUri);
}
///
/// Sends a GET request to the specified Uri and return the response body as a string.
/// This extension is only used in frameworks prior to .NET 5.0 where CancellationToken is not supported.
///
public static Task GetStringAsync(this HttpClient client, string requestUri, CancellationToken _)
{
// In older frameworks, we ignore the cancellation token since it's not supported
return client.GetStringAsync(requestUri);
}
///
/// Sends a GET request to the specified Uri and return the response body as a string.
/// This extension is only used in frameworks prior to .NET 5.0 where CancellationToken is not supported.
///
public static Task GetStringAsync(this HttpClient client, Uri requestUri, CancellationToken _)
{
// In older frameworks, we ignore the cancellation token since it's not supported
return client.GetStringAsync(requestUri);
}
///
/// Sends a POST request to the specified Uri as an asynchronous operation.
/// This extension is only used in frameworks prior to .NET 5.0 where CancellationToken is not supported.
///
public static Task PostAsync(this HttpClient client, string requestUri, HttpContent content, CancellationToken _)
{
// In older frameworks, we ignore the cancellation token since it's not supported
return client.PostAsync(requestUri, content);
}
///
/// Sends a POST request to the specified Uri as an asynchronous operation.
/// This extension is only used in frameworks prior to .NET 5.0 where CancellationToken is not supported.
///
public static Task PostAsync(this HttpClient client, Uri requestUri, HttpContent content, CancellationToken _)
{
// In older frameworks, we ignore the cancellation token since it's not supported
return client.PostAsync(requestUri, content);
}
///
/// Sends an HTTP request as an asynchronous operation.
/// This extension is only used in frameworks prior to .NET 5.0 where CancellationToken is not supported.
///
public static Task SendAsync(this HttpClient client, HttpRequestMessage request, CancellationToken _)
{
// In older frameworks, we ignore the cancellation token since it's not supported
return client.SendAsync(request);
}
}
///
/// Extension methods for HttpContent to provide CancellationToken support in frameworks before .NET 5.0.
///
internal static class HttpContentExtensions
{
///
/// Serialize the HTTP content to a string as an asynchronous operation.
/// This extension is only used in frameworks prior to .NET 5.0 where CancellationToken is not supported.
///
public static Task ReadAsStringAsync(this HttpContent content, CancellationToken _)
{
// In older frameworks, we ignore the cancellation token since it's not supported
return content.ReadAsStringAsync();
}
}
#endif