Advertisement
sword_smith

Mulig løsning til at logge HTTP headers

Nov 28th, 2020
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. public class LoggingHandler : DelegatingHandler
  2.     {
  3.         public LoggingHandler(HttpMessageHandler innerHandler)
  4.             : base(innerHandler)
  5.         {
  6.         }
  7.  
  8.         protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
  9.         {
  10.             Console.WriteLine("Request:");
  11.             Console.WriteLine(request.ToString());
  12.             if (request.Content != null)
  13.             {
  14.                 Console.WriteLine(await request.Content.ReadAsStringAsync());
  15.             }
  16.             Console.WriteLine(request.Headers.ToString());
  17.  
  18.             HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
  19.  
  20.             Console.WriteLine("Response:");
  21.             Console.WriteLine(response.ToString());
  22.             if (response.Content != null)
  23.             {
  24.                 Console.WriteLine(await response.Content.ReadAsStringAsync());
  25.             }
  26.             Console.WriteLine();
  27.  
  28.             return response;
  29.         }
  30.     }
  31.  
  32. HttpClient hotWallet = new HttpClient(new LoggingHandler(new HttpClientHandler()))
  33. {
  34.     BaseAddress = hwBaseUrl,
  35. };
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement