且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获得域名基础上的IP地址

更新时间:2023-02-05 09:28:43

我猜你在谈论从主机名得到的***的域名?该***域名是完整的主机名只是最后两个点分部分,因此函数是这样的:

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.