获得域名基础上的IP地址基础上、地址、域名、IP

2023-09-03 21:01:49 作者:吻,离别滋味

我需要一个域名,如果我有一个IP地址(例如I型209.85.129.103和程序应该找出是谷歌地址)

I need to get a domain name if I have an IP address (e.g. 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;
}

如果你实际上是试图找出谁的拥有的领域,你必须使用一个WHOIS查询。这里有一个在C#的whois的例子。这些信息只是回来为纯文本;请记住,这并不一定,甚至告诉你真正的人或公司谁拥有它,有时候这些信息是私人的,所有你得到的是注册商(如GoDaddy的)。

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).

此外,不同的域名注册服务器会给出不同的信息和不同的地区;比如你可以得到与ARIN一个美域的信息,但对欧洲域名您需要使用RIPE代替。老实说,我希望这是你想要做的,因为你会发现,这是一个相当焦油坑不算什么;有可靠确定的域X 的是拥有没有简单的方法的 Y公司的。

Also, different whois servers will give different information and different areas; for example you can get information on a U.S. 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.