我如何从一个401服务器错误头 [英] How do I get headers from a 401 Server Error
问题描述
我正在写一个端口扫描检测本地网络上运行的Web服务。其中的一些网络服务需要基本身份验证 - 我不知道用户名/密码为这些服务,我只是想一一列举,所以在这个阶段,我不能提供凭据。我使用的是code:
I'm writing a port scanner to detect web services running on the local network. Some of these web services require basic authentication - I don't know the username/ password for these services, I just want to list them, so I can't provide the credentials at this stage. I'm using the code:
var request = (HttpWebRequest)WebRequest.Create("http://" + req);
request.Referer = "";
request.Timeout = 3000;
request.UserAgent = "Mozilla/5.0";
request.AllowAutoRedirect = false;
request.Method = WebRequestMethods.Http.Head;
HttpWebResponse response = null;
try
{
response = (HttpWebResponse) request.GetResponse();
// I want to parse the headers here for the server name but as the exception is thrown the response object is null.
}
catch (Exception ex)
{
//401 error is caught here - response is null
}
我再解析出服务器的名字从返回的头 - 我知道他们正在返回,因为我可以看到他们与小提琴手,但HttpWebResponse对象设置为null作为的GetResponse()方法抛出异常。基本上 - 我怎么得到它不抛出和异常,但随着401状态code返回头
I'm then parsing out the server name from the headers that are returned - I know they are being returned because I can see them with fiddler but the HttpWebResponse object is set to null as the GetResponse() method is throwing an exception. Basically - how do I get it to not throw and exception but return the headers along with a status code of 401.
推荐答案
如果你发现一个 WebException
,你将有机会获得 ex.Response
,并从那里你可以检索你的头。
If you catch a WebException
you'll have access to ex.Response
and you can retrieve your headers from there.
这篇关于我如何从一个401服务器错误头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!