我使用以下代码从网址获取授权标头,但授权字段始终返回为空
string url = "https://xyz.appdirect.com/api/integration/v1/events/abc-123";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
string value = httpWebRequest.Headers["Authorization"];
当我按照MSDN代码时,我得到了标题
的空值 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"));
}
获取授权标头的正确方法是什么?
答案 0 :(得分:0)
简单的解决方案是从HttpContext
获取标题,但我将其复制到其他内容而不是直接从HttpContext
读取。
NameValueCollection authHeader = HttpContext.Current.Request.Headers;
foreach (string strKey in authHeader)
{
_logger.Info(String.Format(" Key Value header: {0}", authHeader[strKey]));
}