简体   繁体   中英

Get a domain name basing on the IP address

I need to get a domain name if I have an IP address (eg I type 209.85.129.103 and the program should find out that is the Google address)

As far as I've figured out is get the hostname:

IPHostEntry IpToDomainName = Dns.GetHostEntry("209.85.129.103");
string HostName = IpToDomainName.HostName; //it returns "fk-in-f103.1e100.net"

but it's not that I want. I don't know how to achieve that. Any ideas will be helpful

I guess you're talking about getting the top-level domain name from the host name? The TLD is just the last two dot-separated parts of the full host name, so a function would look like this:

public static string GetTopLevelDomain(string hostName)
{
    int lastDot = hostName.LastIndexOf('.');
    if (lastDot < 0)
        return hostName;
    int previousDot = hostName.LastIndexOf('.', lastDot - 1);
    return (previousDot >= 0) ? hostName.Substring(previousDot + 1) : hostName;
}

If you're actually trying to figure out who owns the domain, you have to use a whois lookup. Here's a whois example in C# . The information just comes back as plain text; keep in mind that it won't necessarily even tell you the real person or company who owns it, sometimes that information is private and all you'll get is the registrar (like GoDaddy).

Also, different whois servers will give different information and different areas; for example you can get information on a US domain with ARIN, but for European domains you need to use RIPE instead. Honestly I hope that this isn't what you're trying to do because you're going to find that it's a quite a tar-pit; there's no simple way of reliably determining that Domain X is owned by Company Y .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM