How to dump HttpClient request in a text file?

2019-04-17 05:08发布

问题:

I'm sending an HTTP request to an endpoint, using HttpClient class in C#.

The problem is that using Fiddler everything works fine, but using C#, something goes wrong and I can't tell what.

Is there a way to dump HTTP request of HttpClient instead of sending it to the server, just like how Fiddler captures traffic, so that we can see what's going on?

PCap is not my solution. I don't want to do it everywhere. I want to be able to somehow dump my single current instance of HttpClient.

For example, let's suppose this code:

var client = new HttpClient();
// client.PostAsync(… 
// client.LogPostingAsync(…

Update: (x-y problem clarification) I'm using Fiddler to test the API, and it works. Here are the parameters:

  1. should be POST
  2. Content-Type should be set to application/json
  3. Request body should be JSON

With the same parameters in my C# code, this piece of code causes error:

var parameters = new Parameters();
// setting properties
using (var http = new HttpClient())
{
    var response = http.PostAsJsonAsync(url, parameters).Result;
    // not getting the same response as Fiddler test, how to debug?
}