如何监控HttpClientHandler中的所有请求?

时间:2018-04-04 13:21:11

标签: c# .net

我试图调试一些HttpClient身份验证问题,所以我有:

var handler = new MyHttpClientHandler { Credentials = new NetworkCredential("myun", "mypw") };

using (var client = new HttpClient(handler))
{
    await client.GetStringAsync("https://httpbin.org/digest-auth/auth/myun/mypw");
}

其中:

public class MyHttpClientHandler : HttpClientHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var result = base.SendAsync(request, cancellationToken);
        return result;
    }
}

但SendAsync()只被命中一次,其中看到请求包含没有身份验证详细信息的原始请求,结果包含成功身份验证的最终200响应。我怎样才能看到最初的401质询和后续请求,其中包含必须在中间发生的凭证?

0 个答案:

没有答案