从URL获取域,而不具有子域 [英] Get domain without subdomain from a URL

查看:139
本文介绍了从URL获取域,而不具有子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



问题是因为可以有如下主机:
subhost.example.com

subhost.example.co.uk



这两个部分域中有其他几个类似co.uk(请参阅 https://wiki.mozilla.org/TLD_List )。



在我看来,只有域名唯一正确的方式是通过TLD列表进行搜索,从主机的末尾删除TLD,并在主机的最后一段时间之前取走所有内容。有现有的方法吗?我没有在java.net.URL中看到一个,我检查了apache commons,但是找不到一个。

解决方案

我知道这是迟了几年,但如果有人绊倒这个问题,请尝试以下操作:

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

以上将返回示例.co.uk。


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

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

The problem is because there can be hosts like: subhost.example.com and 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).

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.

解决方案

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.

这篇关于从URL获取域,而不具有子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆