我正在做一个简单的 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();
}
}
我正在寻找响应中的自定义 auth-header,但是当我遍历 Headers
集合时它丢失了。
但是,Fiddler
捕获的相同 Http 请求显示 header 。
如果我在 Postman
或任何浏览器上发出相同的请求,我会看到 header 。
想知道我在这里错过了什么。
[更新]
通过执行上面相同的代码在 Fiddler
上捕获原始 header :
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
我想捕获 header WWW-Authenticate-Test
,它在通过 HttpClient
魔法时以某种方式被过滤掉。
最佳答案
它可能是一个 content header :
response.Content.Headers.Select(header => header.Key).Dump();
关于c# - HttpResponseMessage 在使用 HttpClient 时缺少 header ,但 fiddler 找到了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47242412/