Skip to content

Instantly share code, notes, and snippets.

@ChenTanyi
Created September 10, 2021 07:40
Show Gist options
  • Save ChenTanyi/f10489a777529997672febd31bc04706 to your computer and use it in GitHub Desktop.
Save ChenTanyi/f10489a777529997672febd31bc04706 to your computer and use it in GitHub Desktop.
debug http client of c#
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;
}
}
}
@amingolmahalle
Copy link

amingolmahalle commented Mar 10, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment