c# - HttpResponseMessage 在使用 HttpClient 时缺少 header ,但 fiddler 找到了

标签 c# httpclient

我正在做一个简单的 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/

相关文章:

c# - HttpClient Async 方法会在新线程中运行吗

c# - 在不使用 RouteAttribute 的情况下使用 ApiControllerAttribute

c# - Linq any - 如何选择

c# - 如何在 Windows Phone 应用程序 (C#) 中显示上传进度?

android - 在 Android 上使用带有 DefaultHttpClient 的 HttpPost 时出现身份验证错误

java - 通过Java执行http客户端JSONObject文本格式错误

c# - 如何使用 Asp.Net Web API 将对象设置为 JSON 字符串中的值?

c# - 对log4net功能的困惑,请帮忙澄清一下?

c# - ASP.NET Core 中的 key 加密

.net-core - 如何在 .NET Core 3 中不使用 RestSharp 代理