java 从 URL 获取没有子域的域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3199862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:46:36  来源:igfitidea点击:

Get domain without subdomain from a URL

javadnssubdomain

提问by James Smith

What is the proper way to get the domain from a URL without the subdomains?

从没有子域的 URL 获取域的正确方法是什么?

In Java, from a string you can make a new URL(urlString) and call getHost() on the URL, but you have subdomains with it.

在 Java 中,您可以从字符串创建一个新的 URL(urlString) 并在该 URL 上调用 getHost(),但是您有子域。

The problem is because there can be hosts like: subhost.example.com and subhost.example.co.uk

问题是因为可以有这样的主机: subhost.example.com 和 subhost.example.co.uk

There are several other of these two part domains like co.uk (see the list on https://wiki.mozilla.org/TLD_List).

这两个部分中还有其他几个域,例如 co.uk(请参阅https://wiki.mozilla.org/TLD_List上的列表)。

It seems to me the only correct way to get only the domain is to do a search through the TLD list, remove the TLD from the end of the host, and take away everything before the last period in the host. Is there an existing method that does this? I didn't see one in java.net.URL, and I checked apache commons a bit but couldn't find one there.

在我看来,仅获取域的唯一正确方法是通过 TLD 列表进行搜索,从主机的末尾删除 TLD,并在主机的最后一段之前删除所有内容。是否有一种现有的方法可以做到这一点?我没有在 java.net.URL 中看到一个,我检查了 apache commons,但在那里找不到一个。

回答by Dan

I know this is a few years late but if anyone stumbles across this question try the following:

我知道这晚了几年,但如果有人偶然发现这个问题,请尝试以下操作:

InternetDomainName.from("subhost.example.co.uk").topPrivateDomain().name

The above will return example.co.uk.

以上将返回example.co.uk。

回答by Tinus Tate

Not sure if the above answer is correct:

不确定上面的答案是否正确:

InternetDomainName.from("test.blogspot.com").topPrivateDomain() -> test.blogspot.com

This works better in my case:

这在我的情况下效果更好:

InternetDomainName.from("test.blogspot.com").topDomainUnderRegistrySuffix() -> blogspot.com

Details: https://github.com/google/guava/wiki/InternetDomainNameExplained

详情:https: //github.com/google/guava/wiki/InternetDomainNameExplained