locked
How to get specific Header value in a Http.GetAsync call RRS feed

  • Question

  • User-2015242085 posted

    Hi

    How to get specific Header value in a Http.GetAsync call

    public async void GetDetailsByKey(string Key)
    {
                try
                {
                    string URL = "https://website.com/";
                    HttpClient Http = new HttpClient();
                    HttpResponseMessage response = Http.GetAsync((URL + ("details/" + Key))).Result;
                    string res = await response.Content.ReadAsStringAsync();
                    var AllHeaders = response.Headers.ToString();
                    jsonDetails = res.ToString()  + "<br/><br/>Headers: " + AllHeaders;
                }
                catch
                {
                    jsonDetails = "Error!";
                }
    }

    I can get all the headers like:

    var AllHeaders = response.Headers.ToString();

    But how to get a single header like "Header Name"

    Thursday, September 13, 2018 6:12 AM

Answers

  • User1724605321 posted

    Hi Primillo,

    Please refer to this thread : https://stackoverflow.com/questions/25439421/how-to-get-an-specific-header-value-from-the-httpresponsemessage 

    HttpHeaders headers = response.Headers;
    IEnumerable<string> values;
    if (headers.TryGetValues("X-BB-SESSION", out values))
    {
      string session = values.First();
    }

    You can check other available solutions in that thread . 

    If your requirement is to get specific header ,you can loop Request.Headers , for example :

    string headers = String.Empty;
    foreach (var key in Request.Headers.AllKeys)
      headers += key + "=" + Request.Headers[key] + Environment.NewLine;

    You can add if...else statement to get specific header key/name .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 13, 2018 6:48 AM

All replies

  • User1724605321 posted

    Hi Primillo,

    Please refer to this thread : https://stackoverflow.com/questions/25439421/how-to-get-an-specific-header-value-from-the-httpresponsemessage 

    HttpHeaders headers = response.Headers;
    IEnumerable<string> values;
    if (headers.TryGetValues("X-BB-SESSION", out values))
    {
      string session = values.First();
    }

    You can check other available solutions in that thread . 

    If your requirement is to get specific header ,you can loop Request.Headers , for example :

    string headers = String.Empty;
    foreach (var key in Request.Headers.AllKeys)
      headers += key + "=" + Request.Headers[key] + Environment.NewLine;

    You can add if...else statement to get specific header key/name .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 13, 2018 6:48 AM
  • User-2015242085 posted

    Hi Nan Yu

    I already tried that and I get an error on the line:

    string session = values.First();

    I get ...  IEnumerable<string> does not contain a definition for "First"

    And the second code sample don't work

    Any other idea on how to get just a specific header from the headers collection?

    Best Regards

    Thursday, September 13, 2018 7:16 AM
  • User-2015242085 posted

    THX Nan Yu

    You were right!

    I just was missing:

    using System.Linq;

    This code do the job!

    HttpHeaders headers = response.Headers;
    string session = "";
    IEnumerable<string> values;
    if (headers.TryGetValues("X-Error-Cause", out values))
    {
        session = values.First();
    }

    Regards

    Thursday, September 13, 2018 7:29 AM