我已经为 Firefox 和 Chrome 测试了一些插件,它们可以识别给定网站的 IP 号码。但其中一些还可以显示网站运行的服务器端技术。
他们怎么做到的?我知道客户端-用户-代理,在服务器发送“服务器-主机-代理”字符串的 HTTP 协议中是否有类似的东西?
如果是这样,检索此外观的代码将如何。我猜它与WebClient有关?
任何人?
我已经为 Firefox 和 Chrome 测试了一些插件,它们可以识别给定网站的 IP 号码。但其中一些还可以显示网站运行的服务器端技术。
他们怎么做到的?我知道客户端-用户-代理,在服务器发送“服务器-主机-代理”字符串的 HTTP 协议中是否有类似的东西?
如果是这样,检索此外观的代码将如何。我猜它与WebClient有关?
任何人?
使用HttpWebRequest
并将Method
属性设置为HEAD
,您可以进行 HTTP HEAD 请求,非常轻量级。它将返回 HTTP 标头(可能正确也可能不正确)。它们的 HTTP 标头也可能因服务器而异,因为对于服务器应公开的标头没有标准。
编码:
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/");
myReq.Method = "HEAD";
WebResponse myRes = myReq.GetResponse();
for(int i=0; i < myHttpWebResponse.Headers.Count; ++i) {
Console.WriteLine(
"\nHeader Name:{0}, Value :{1}",
myHttpWebResponse.Headers.Keys[i], myHttpWebResponse.Headers[i]
);
}
编辑:
var request = (HttpWebRequest)WebRequest.Create("http://www.http500.com");
try
{
var response = request.GetResponse();
}
catch (WebException wex)
{
// Safe cast to HttpWebResponse using 'as', will return null if unsuccessful
var httpWebResponse = wex.Response as HttpWebResponse;
if(httpWebResponse != null)
{
var httpStatusCode = httpWebResponse.StatusCode;
// HttpStatusCode is an enum, cast it to int for its actual value
var httpStatusCodeInt = (int)httpWebResponse.StatusCode;
}
}