1

I'm trying to create a custom message handler which adds an Authorization header to all requests sent by an HttpClient. In order to compute the value of this header, the path of the request (including query parameters) must be concatenated with the the body of the request (which must have a content type of application/x-www-form-urlencoded).

Since my use-case is very latency sensitive, I was wondering whether the code below is the most efficient/ fastest way of doing this? Or could it be improved? In particular, is there a better alternative to having to use ReadAsStringAsync to get the form data?

class AuthorizationHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request,
        CancellationToken cancellationToken)
    {
        var path = request.RequestUri.PathAndQuery;
        var content = await request.Content.ReadAsStringAsync(cancellationToken);
        // TODO: create "Authorization" header
        return await base.SendAsync(request, cancellationToken);
    }
}
jack
  • 147
  • 1
  • 7

0 Answers0