How to dump HttpClient request in a text file?

空扰寡人 提交于 2019-12-08 13:50:14

问题


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?
}

来源:https://stackoverflow.com/questions/53081428/how-to-dump-httpclient-request-in-a-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!