首页 > 解决方案 > await HttpClient.SendAsync 没有响应

问题描述

我有一个返回输出的 curl 命令。Access Denied 对我来说很好,XML 输出是我目前感兴趣的。

在此处输入图像描述

curl -v  -E C:\SEB\certificate.pfx --cert-type p12 -X GET https://test.api.bgw.baltics.sebgroup.com/v1/accounts/EE101010220279354221/current-transactions -H OrgId:22223338

我编写了一个代码片段,它应该与提供的 curl 命令执行相同的操作,但响应变量返回 null (当代码到达 line 时)

response = await httpClient.SendAsync(request); 

程序退出,不再执行任何代码。

我究竟做错了什么?为什么我不能像 curl 命令一样获取响应的内容?

提前谢谢你的帮助


public async Task HttpRequestAppAsync()
{
    var handler = new HttpClientHandler {ClientCertificateOptions = ClientCertificateOption.Manual};
    handler.ClientCertificates.Add(new X509Certificate2("C:\\SEB\\certificate.pfx"));

    using (var httpClient = new HttpClient(handler))
    {
        using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://test.api.bgw.baltics.sebgroup.com/v1/accounts/EE101010220279354221/current-transactions"))
        {
            request.Headers.TryAddWithoutValidation("OrgId", "22223338");
            response = await httpClient.SendAsync(request);
        }
    }  
}

标签: c#httpcurlx509

解决方案


似乎已经想通了。死锁到位 我需要做的就是添加一个额外的方法 ConfigureAwait 并将其设置为 false

response = await httpClient.SendAsync(request).ConfigureAwait(false);

推荐阅读