1

I'm using the below code to get the authorization header from a url but the authorization field is always returned as null

 string url = "https://xyz.appdirect.com/api/integration/v1/events/abc-123";
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            string value = httpWebRequest.Headers["Authorization"];

Also when i followed the MSDN code, i am getting a null value for the headers

  WebRequest request = WebRequest.Create(url);
            request.Method = WebRequestMethods.Http.Get;
            NameValueCollection authHeader = request.Headers;

            if (authHeader.Count > 0)
            {
                foreach (string strKey in authHeader)
                {
                    string s = strKey + " = " + request.Headers[strKey] + "<br />\n";
                    Console.WriteLine(String.Format(" Key Value header: {0}", authHeader[strKey]));

                }
            }
            else
            {
                Console.WriteLine(String.Format("No headers found"));

            }

What is the correct way to get the authorization header?

Yoda
  • 319
  • 2
  • 5
  • 20

1 Answers1

0

The simple solution is to get the headers from HttpContext, but I complicated it doing other things rather than reading it directly from the HttpContext.

NameValueCollection authHeader = HttpContext.Current.Request.Headers;
foreach (string strKey in authHeader)
{
    _logger.Info(String.Format(" Key Value header: {0}", authHeader[strKey]));
}
Ahmad Khan
  • 2,655
  • 19
  • 25
Yoda
  • 319
  • 2
  • 5
  • 20