我在Windows 8应用程序中使用HttpClient的,似乎是故意隐藏的自定义标题中的响应。例如:
I'm using HttpClient in a Windows 8 app and it seems that it purposely hides custom headers in the response. For example:
我们的接收到的响应有一个名为采样头:123自定义标题
Our response received has a custom header called "Sample-Header: 123"
我预计,在响应内容头将包含采样头与123
I expect that the headers in the response content would contain "Sample-Header" with a value of "123"
var client = new HttpClient();
var response = await client.GetAsync(uri);
string sample;
IEnumerable<string> values;
if (response.Content.Headers.TryGetValues("Sample-Header", out values))
{
// This never happens!
sample = values.First();
}
即使我在头枚举,我永远也找不到我们的自定义标题。
Even if I enumerate through the headers, I'll never find our custom headers.
确定。显然,有可以使用两个不同的标题的集合。下面code工作:
Ok. Apparently, there are two different header collections you could use. The following code works:
var client = new HttpClient();
var response = await client.GetAsync(uri);
string sample;
IEnumerable<string> values;
if (response.Headers.TryGetValues("Sample-Header", out values))
{
// This happens!
sample = values.First();
}
你看到的区别?内容头是从响应头完全不同。
Do you see the difference? Content headers are completely different from response headers.
由于戈文德来自微软。