开发者

How do plugins identify what type of server a website is running on?

I have tested a few plugins for Firefox and Chrome which can identify IP number of a g开发者_如何学Civen website ofcause. But some of them can also show what server-side technology the website runs on.

How do they do this? I know about client-user-agent, is there something similar in the HTTP protocol where the server sendes a "server-host-agent" kinda string?

And if so, how would the code for retreiving this look. I guess its something with WebClient?

Anyone?


Using the HttpWebRequest and setting the Method property to HEAD, you can do a HTTP HEAD request, which is very lightweight. It will return the HTTP Headers (which may or may not be correct). They HTTP Headers may also differ from server to server as there is no standard for what headers a server should expose.

The code:

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]
   ); 
}

EDIT:

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;                    
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜