HttpMessageHandler Class
Provides a base class for sending and receiving HTTP requests and responses. This is an abstract class and must be inherited by a concrete class.
Syntax
public abstract class HttpMessageHandler : IDisposable
Inheritance
- Object
- HttpMessageHandler
Remarks
The HttpMessageHandler class is the base class for all HTTP message handlers. It provides a mechanism to send HTTP requests and receive HTTP responses. It is an abstract class, meaning you cannot instantiate it directly. You must create a concrete class that derives from HttpMessageHandler and implements its abstract methods.
The primary method to override in a derived class is SendAsync(HttpRequestMessage, CancellationToken). This method is responsible for processing the outgoing HTTP request and returning the incoming HTTP response.
Common derived classes include HttpClientHandler, which provides basic HTTP handling, and custom handlers that can be chained together to implement specific functionalities like logging, authentication, or request modification.
Methods
The HttpMessageHandler class has the following public members:
Dispose()
Releases the unmanaged resources used by the HttpMessageHandler and optionally disposes of the managed resources.
protected virtual void Dispose(bool disposing)
SendAsync()
Sends an HTTP request to the Internet and returns the HTTP response.
protected abstract Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
Parameters
| Name | Type | Description |
|---|---|---|
| request | HttpRequestMessage |
The HTTP request message to send. |
| cancellationToken | CancellationToken |
A token to cancel the operation. |
Returns
| Type | Description |
|---|---|
Task<HttpResponseMessage> |
The HTTP response message received from the server. |
Example
Creating a Custom HttpMessageHandler
The following example demonstrates how to create a simple custom HTTP message handler that logs the request and response.
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
public class LoggingHttpMessageHandler : HttpMessageHandler
{
private readonly HttpMessageHandler _innerHandler;
public LoggingHttpMessageHandler(HttpMessageHandler innerHandler)
{
_innerHandler = innerHandler ?? throw new ArgumentNullException(nameof(innerHandler));
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Console.WriteLine($"Outgoing Request: {request.Method} {request.RequestUri}");
// You can add request headers logging here if needed
HttpResponseMessage response = await _innerHandler.SendAsync(request, cancellationToken);
Console.WriteLine($"Incoming Response: {response.StatusCode} for {request.RequestUri}");
// You can add response headers and content logging here if needed
return response;
}
// Implement IDisposable if your handler manages its own unmanaged resources
protected override void Dispose(bool disposing)
{
if (disposing)
{
_innerHandler.Dispose();
}
base.Dispose(disposing);
}
}
// Usage example:
public class Example
{
public static async Task Main()
{
var httpClientHandler = new HttpClientHandler(); // Base handler
var loggingHandler = new LoggingHttpMessageHandler(httpClientHandler); // Custom handler
using (var client = new HttpClient(loggingHandler))
{
try
{
var response = await client.GetAsync("https://www.example.com");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine("Content received successfully.");
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
}
}
}
}
Requirements
Namespace: System.Net.Http
Assembly: System.Net.Http.dll