Created
September 10, 2021 07:40
-
-
Save ChenTanyi/f10489a777529997672febd31bc04706 to your computer and use it in GitHub Desktop.
debug http client of c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.IdentityModel.Clients.ActiveDirectory; | |
namespace test | |
{ | |
public class HttpClientFactory : IHttpClientFactory | |
{ | |
public HttpClient GetHttpClient() | |
{ | |
return new HttpClient(new LoggingHandler | |
{ | |
InnerHandler = new HttpClientHandler | |
{ | |
ServerCertificateCustomValidationCallback = (a, b, c, d) => true, | |
} | |
}); | |
} | |
} | |
public class LoggingHandler : DelegatingHandler | |
{ | |
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
Console.WriteLine("Request:"); | |
Console.WriteLine(request.ToString()); | |
if (request.Content != null) | |
{ | |
Console.WriteLine(await request.Content.ReadAsStringAsync()); | |
} | |
Console.WriteLine(); | |
HttpResponseMessage response = await base.SendAsync(request, cancellationToken); | |
Console.WriteLine("Response:"); | |
Console.WriteLine(response.ToString()); | |
if (response.Content != null) | |
{ | |
Console.WriteLine(await response.Content.ReadAsStringAsync()); | |
} | |
Console.WriteLine(); | |
return response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, dear @ChenTanyi . I have written a package for getting the curl script of HttpClient called HttpClientToCurl you can see its sources in GitHub: HttpClientToCurl
It's so suitable for debugging the HttpClient.
I will be happy if you inform me if you have any feedback and your solution to improve the code.
Thank you.