HttpManager Class
Definition
Provides ways for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.
public class HttpManager : Disposable, IDisposable
- Inheritance
-
HttpManager
- Implements
- Inherited Members
Examples
HttpManager provides HTTP operation methods backed by a configurable IHttpClientFactory. This example creates an HttpManager with a custom EchoHandler that returns 200 OK for all requests, then calls HttpGetAsync on a test URI. Key setup includes passing a factory delegate that returns HttpClient instances backed by the echo handler. Console output confirms the response status code (OK) and that Timeout is greater than TimeSpan.Zero.
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Cuemon.Net.Http;
namespace MyApp.Examples;
public static class HttpManagerExample
{
public static async Task DemonstrateAsync()
{
using var manager = new HttpManager(() => new HttpClient(new EchoHandler(), false));
using var response = await manager.HttpGetAsync(new Uri("https://example.com/health"));
Console.WriteLine(response.StatusCode);
Console.WriteLine(manager.Timeout > TimeSpan.Zero);
}
private sealed class EchoHandler : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
{
RequestMessage = request,
Content = new StringContent("OK")
});
}
}
}
Constructors
| Name | Description |
|---|---|
| HttpManager(Action<HttpManagerOptions>) | Initializes a new instance of the HttpManager class. |
| HttpManager(Func<HttpClient>) | Initializes a new instance of the HttpManager class. |
Properties
| Name | Description |
|---|---|
| DefaultRequestHeaders | Gets the headers which should be sent with each request. |
| Timeout | Gets or sets the timespan to wait before the request times out. |
Methods
| Name | Description |
|---|---|
| HttpAsync(HttpMethod, Uri, MediaTypeHeaderValue, Stream, CancellationToken) | Send a request as an asynchronous operation. |
| HttpAsync(HttpMethod, Uri, string, Stream, CancellationToken) | Send a request as an asynchronous operation. |
| HttpAsync(Uri, Action<HttpRequestOptions>) | Send a request as an asynchronous operation. |
| HttpDeleteAsync(Uri, CancellationToken) | Send a DELETE request to the specified Uri as an asynchronous operation. |
| HttpGetAsync(Uri, CancellationToken) | Send a GET request to the specified Uri as an asynchronous operation. |
| HttpHeadAsync(Uri, CancellationToken) | Send a HEAD request to the specified Uri as an asynchronous operation. |
| HttpOptionsAsync(Uri, CancellationToken) | Send an OPTIONS request to the specified Uri as an asynchronous operation. |
| HttpPatchAsync(Uri, MediaTypeHeaderValue, Stream, CancellationToken) | Send a PATCH request to the specified Uri as an asynchronous operation. |
| HttpPatchAsync(Uri, string, Stream, CancellationToken) | Send a PATCH request to the specified Uri as an asynchronous operation. |
| HttpPostAsync(Uri, MediaTypeHeaderValue, Stream, CancellationToken) | Send a POST request to the specified Uri as an asynchronous operation. |
| HttpPostAsync(Uri, string, Stream, CancellationToken) | Send a POST request to the specified Uri as an asynchronous operation. |
| HttpPutAsync(Uri, MediaTypeHeaderValue, Stream, CancellationToken) | Send a PUT request to the specified Uri as an asynchronous operation. |
| HttpPutAsync(Uri, string, Stream, CancellationToken) | Send a PUT request to the specified Uri as an asynchronous operation. |
| HttpTraceAsync(Uri, CancellationToken) | Send a TRACE request to the specified Uri as an asynchronous operation. |
| OnDisposeManagedResources() | Called when this object is being disposed by either Dispose() or Dispose(bool) having |