HttpClient missing response headers in WinRT / Win8

浪子不回头ぞ 提交于 2019-12-11 03:58:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!