T4m*_*mer 2 c# webresponse httpwebrequest basic-authentication
如何读取请求HTTP基本身份验证的服务器在WWW-Authenticate标头中发送的Realm属性?
不确定选民对这个问题的看法究竟是什么问题.
这是获取包含基本身份验证领域的WWW-Authenticate标头的粗略代码.从标题中提取实际领域值是一个练习,但应该非常简单(例如使用正则表达式).
public static string GetRealm(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
try
{
using (request.GetResponse())
{
return null;
}
}
catch (WebException e)
{
if (e.Response == null) return null;
var auth = e.Response.Headers[HttpResponseHeader.WwwAuthenticate];
if (auth == null) return null;
// Example auth value:
// Basic realm="Some realm"
return ...Extract the value of "realm" here (with a regex perhaps)...
}
}
Run Code Online (Sandbox Code Playgroud)