How to get an specific header value from the HttpResponseMessage

前端 未结 7 1827
挽巷
挽巷 2020-12-16 09:17

I\'m making an HTTP call. My response contains a session code X-BB-SESSION in the header section of the HttpResponseMessage object. How do I get th

7条回答
  •  悲&欢浪女
    2020-12-16 09:28

    You are trying to enumerate one header (CacheControl) instead of all the headers, which is strange. To see all the headers, use

    foreach (var value in responseHeadersCollection)
    {
        Debug.WriteLine("CacheControl {0}={1}", value.Name, value.Value);
    }
    

    to get one specific header, convert the Headers to a dictionary and then get then one you want

    Debug.WriteLine(response.Headers.ToDictionary(l=>l.Key,k=>k.Value)["X-BB-SESSION"]);
    

    This will throw an exception if the header is not in the dictionary so you better check it using ContainsKey first

提交回复
热议问题