【发布时间】:2020-10-27 16:52:20
【问题描述】:
我想连接到 REST Api,但无论我如何构建请求,它都会不断返回 status code 400 (Bad Request)。
HttpResponseMessage response = client.PostAsync(url, content).Result;
response.EnsureSuccessStatusCode();
我的问题是,如何调试我的https 请求到服务器?我想看看我的 HTTP 请求是什么样子的。
我尝试使用DiagnosticSource 类,因此我创建了Observer 类并为所有听众订阅它。
观察者
class Observer : IObserver<DiagnosticListener>
{
public void OnCompleted()
{
throw new NotImplementedException();
}
public void OnError(Exception error)
{
throw new NotImplementedException();
}
public void OnNext(DiagnosticListener value)
{
if (value.Name == "HttpHandlerDiagnosticListener")
{
value.Subscribe(new HttpClientObserver());
}
}
}
HttpClientObserver
class HttpClientObserver : IObserver<KeyValuePair<string, object>>
{
public void OnCompleted()
{
throw new NotImplementedException();
}
public void OnError(Exception error)
{
throw new NotImplementedException();
}
public void OnNext(KeyValuePair<string, object> value)
{
throw new NotImplementedException();
}
}
主要
static void Main(string[] args)
{
DiagnosticListener.AllListeners.Subscribe(new Diagnostic.Observer());
// execute HttpClient methods do not throw any event to Observer
但是没有捕获到任何事件。我错过了什么,HttpClient 是否支持DiagnosticSource(我如何识别哪些课程?)。我还可以使用什么其他解决方案?
【问题讨论】:
-
如果您不是在寻找永久固定装置,而只是为了调试此问题,您可以使用 Fiddler 之类的工具来捕获网络流量并查看所有详细信息。
-
感谢您的提示,但我无法使用这些工具。无论如何,Fiddler 可以检查
https请求吗? -
是的,可以。也是邮递员的拦截器。
-
Burp Suite 还可以跟踪 http/https 流量。
-
你需要等待 PostAsync。例如 HttpResponseMessage response = await client.PostAsync(url, content);
标签: c# console-application dotnet-httpclient system.diagnostics .net-4.6.1