背景
**1.**有个需求需要统计某台机器访问其他网站的频次,同一个主域名下的频次需要合并,即例如访问
mp.csdn.net 和 blog.csdn.net 各一次,应该统计为 csdn.net 两次。
2. 使用dig命令 dig 域名的ns记录时,如果不是主域名,无法正确获取到其nsServer,因此需要提取出其中的主域名
引入guava
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
<scope>compile</scope>
</dependency>
案例
private String dealHost(String host) {
InternetDomainName domainName = InternetDomainName.from(host);
String top = domainName.topPrivateDomain().toString();
return top;
}
测试
public static void mainHostTest(){
String urlstr = "https://abc.news.sina.com.cn/123/ref?free=key&kt=3";
try {
URL url = new URL(urlstr);
String host = url.getHost();
System.out.println("host: "+host);
InternetDomainName domainName = InternetDomainName.from(host);
String top = domainName.topPrivateDomain().toString();
System.out.println("top: "+top);
} catch (Exception e) {
e.printStackTrace();
}
}
结果
host: abc.news.sina.com.cn
top: sina.com.cn