我在打 HTTP 电话。我的响应在 HttpResponseMessage
对象的头部包含一个会话代码 X-BB-SESSION
。如何得到特定的头部值?
我使用 foreach 语句迭代所有的头(MSDN 链路)。然而,编译器一直说这是不可能的:
foreach statement cannot operate on variables of type
System.net.http.headers.cachecontrolheadervalue because
'System.net.http.headers.cachecontrolheadervalue' doesn't contain
a public definition for 'GetEnumerator'
这是我正在尝试的代码:
//Connection code to BaasBox
HttpResponseMessage response = await client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
//get the headers
HttpResponseHeaders responseHeadersCollection = response.Headers;
foreach (var value in responseHeadersCollection.CacheControl) --> HERE
{
string sTemp = String.Format("CacheControl {0}={1}", value.Name, value.Value);
} else
{
Console.WriteLine("X-BB-SESSION: NOT Found");
}
我试图获取值(X-BB-SESSION
值)的头部内容类似于:
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-Requested-With
X-BB-SESSION: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx