0
点赞
收藏
分享

微信扫一扫

java实现已知Host,提取主域名(Guava)


背景

有个需求需要统计某台机器访问其他网站的频次,同一个主域名下的频次需要合并,即例如访问


mp.csdn.net 和 blog.csdn.net 各一次,应该统计为 csdn.net 两次。


引入guava

​​https://mvnrepository.com/artifact/com.google.guava/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

举报

相关推荐

0 条评论