views:

87

answers:

3

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?

A: 

Are you using this same method when using an IP address? This error will be thrown if you don't have "http://" prepended to the IP address.

JohnForDummies
+2  A: 

Otherwise you could lookup the name from the IP address and then use the address in your other calls:

System.Net.Dns.GetHostEntry("127.0.0.1").HostName.ToString()
ho1
+1  A: 

A single IP can serve multiple domains. You will not have a 1:1 mapping. What you are trying to do is a reverse DNS lookup. There are many webservices that provide an incomplete list of domains that are served from a IP. Once I had to use a combination of them to get a more complete list.

Here is a small list of such sites:

  1. http://remote.12dt.com/
  2. http://www.guerrilladns.com/

And I already used the following DNS lookup that also finds other domains served by the same IP:

  1. http://www.robtex.com/dns/
Jader Dias