使用HttpClient时,HttpResponseMessage缺少标头,但Fiddler可以找到它。

5

我正在进行一个简单的HttpClient调用,如下:

Uri basePath = new Uri("https://my-host.com/");
string path = "api/my-path";

using (HttpClient httpClient = new HttpClient())
{
    httpClient.BaseAddress = basePath;

    HttpResponseMessage response = await httpClient.GetAsync(path);
    if (response.IsSuccessStatusCode)
    {
        response.Headers.Select(header => header.Key).Dump();
    }
}

我正在寻找响应中的自定义认证头,但在迭代 Headers 集合时它丢失了。
然而,在被Fiddler捕获的相同Http请求中,显示出该标头。如果我在 Postman 或任何浏览器上进行相同的请求,则会看到标头。
想知道我错过了什么。
[更新]
通过执行上述相同代码在Fiddler上捕获的原始标头:
HTTP/1.1 302 Found
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Location: <redacted>
Server: Microsoft-IIS/8.0
WWW-Authenticate-Test: <redacted>
X-AspNet-Version: 4.0.30319
Set-Cookie: <redacted>
X-UA-Compatible: IE=edge
Date: Sat, 11 Nov 2017 23:15:19 GMT
Content-Length: 0

上述代码输出的报头内容为:

Pragma 
Strict-Transport-Security 
X-Content-Type-Options 
X-Frame-Options 
x-ms-request-id 
Cache-Control 
P3P 
Set-Cookie 
Server 
X-Powered-By 
Date

我希望捕获标头WWW-Authenticate-Test,但在经过HttpClient处理时被一些方式过滤掉了。


2
经过更多的研究,我意识到HttpClient在重定向时会从响应中剥离授权头(出于安全考虑)。因此,解决方案似乎是修复服务器以返回200而不是302/301。 - Amit
1个回答

7

这可能是一个内容头

response.Content.Headers.Select(header => header.Key).Dump();

不,响应没有正文,我已经验证了内容头。 - Amit
你能否更新你的问题,包括在Fiddler中看到的头信息 - 这样我们就可以创建复制了... - RB.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接