Get the second level domain of an URL (java)

前端 未结 10 923
长发绾君心
长发绾君心 2020-12-06 01:04

I am wondering if there is a parser or library in java for extracting the second level domain (SLD) in an URL - or failing that an algo or regex for doing the same. For exam

10条回答
  •  感动是毒
    2020-12-06 01:05

    Don't know your purpose but Second-Level Domain may not mean much to you. You probably need to find public suffix and the domain right below it is what you are looking for.

    Apache Http Component (HttpClient 4) comes with classes to handle this,

    org.apache.http.impl.cookie.PublicSuffixFilter
    org.apache.http.impl.cookie.PublicSuffixListParser
    

    You need to download the public suffix list from here,

    http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1

提交回复
热议问题