How to check validity of a subdomain in java? [closed]

谁说胖子不能爱 提交于 2019-12-13 11:29:18

问题


I have a domain abc.jp

For example:
I want to check if this is valid to that I can add another sublevel domain to this example. How to do it? If possible, upload some java codes as reference please?

Thank you


回答1:


Your example ac.jp is a valid domain, but not an IP resolvable domain. So I would suggest a whois server. You would need to keep a number of whois servers if you want to check all domains. If you only do Japan then the below snippet should work™.

import org.apache.commons.net.whois.*;

public class Main {
    public static void main(String[] args) {
        WhoisClient whois = new WhoisClient();
        String whoishost = "whois.jprs.jp";
       // String whoishost = "whois.verisign-grs.com"; // for .com domains
        String domain = "ac.jp";
        try {
            whois.connect(whoishost);
            String whoisData = whois.query(domain);
            if (whoisData.toUpperCase().contains("NO MATCH")){
                System.out.println("Domain is not registered. According to whoishost: " + whoishost);
            }
            else{
                System.out.println("Domain is registered. According to whoishost: " + whoishost);
            }
        } catch (java.io.IOException ioex) {
            System.out.println("failed");
        }
    }
}



回答2:


Guava's InternetDomainName provides a good solution to this problem.

String input = "abc.jp";
InternetDomainName domain = InternetDomainName.from(input);
InternetDomainName subdomain = domain.child("www");
subdomain.toString(); // "www.abc.jp";

If the input domain or the subdomain are invalid you'll get an IllegalArgumentException.

You can also do checks like domain.isUnderPublicSuffix(), depending on how much validation you want.



来源:https://stackoverflow.com/questions/41149429/how-to-check-validity-of-a-subdomain-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!