问题
I'm using HttpClient in a Windows 8 app and it seems that it purposely hides custom headers in the response. For example:
Our response received has a custom header called "Sample-Header: 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.
回答1:
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.
Thanks to Govind from Microsoft.
来源:https://stackoverflow.com/questions/12578022/httpclient-missing-response-headers-in-winrt-win8