Get website's server from IP address

Posted by Steven on Stack Overflow See other posts from Stack Overflow or by Steven
Published on 2010-04-21T14:40:21Z Indexed on 2010/04/21 14:43 UTC
Read the original article Hit count: 539

Filed under:
|
|
|

I have a function that returns a website's server when you enter the url for the site:

private string GetWebServer()
{
    string server = string.Empty;

    //get URL
    string url = txtURL.Text.Trim().ToLower();
    if (!url.StartsWith("http://") && !url.StartsWith("https://"))
        url = "http://" + url;

    HttpWebRequest request = null;
    HttpWebResponse response = null;

    try
    {
        request = WebRequest.Create(url) as HttpWebRequest;
        response = request.GetResponse() as HttpWebResponse;

        server = response.Headers["Server"];
    }
    catch (WebException wex)
    {
        server = "Unknown";
    }
    finally
    {
        if (response != null)
        {
            response.Close();
        }
    }

    return server;
}

I'd like to also be able to get a website's server from the IP address instead of the site's url. But if I enter an IP address, I get an error saying "Invalid URI: The format of the URI could not be determined." when calling WebRequest.Create(url).

Does someone know how I can modify this to accomplish what I want?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#