如何使用java识别URL对象的顶级域?
鉴于此:
URL u=new URL("someURL");
我如何识别 URL 的顶级域..
Given this :
URL u=new URL("someURL");
How do i identify the top level domain of the URL..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Guava 为此提供了一个很好的实用程序。它的工作原理如下:
InternetDomainName.from("someurl.co.uk").publicSuffix()
将为您提供co.uk
InternetDomainName.from("someurl.de").publicSuffix()
将为您提供de
Guava provides a nice utility for this. It works as follow:
InternetDomainName.from("someurl.co.uk").publicSuffix()
will get youco.uk
InternetDomainName.from("someurl.de").publicSuffix()
will get youde
那么您只想拥有顶级域部分?
我们来测试一下吧!
输出:
So you want to have the top-level domain part only?
Let's test it!
Output:
url 的主机部分符合 RFC 2732,根据 文档。这意味着简单地分割从中获得的字符串
是不够的。您需要确保在搜索主机时符合 RFC 2732,或者如果您可以保证所有地址都是 server.com 形式,那么您可以搜索最后一个 .在字符串中并获取 tld。
The host part of the url conforms to RFC 2732 according to the docs. It would imply that simply splitting the string you get from
would not be enough. You will need to ensure that you conform to the RFC 2732 when searching the host OR if you can guarantee that all addresses are of the form server.com then you can search for the last . in the string and grab the tld.
使用
URL# getHost()
,如有必要,此后为String#split()
位于“\\.”
上。更新:如果您确实有一个IP地址作为主机,那么您需要使用
InetAddress#getHostName()
独立。Use
URL#getHost()
and if necessary thereafter aString#split()
on"\\."
.Update: if you actually have an IP address as host, then you need to make use of
InetAddress#getHostName()
independently.