How to Validate a Domain Name in Java
Validate whether a URL is syntactically valid, whether it exists, and whether the endpoint is up and passes virus scan checks.
Join the DZone community and get the full member experience.Join For Free
A domain name is used to represent online entities and provide users with access to websites that can help them accomplish goals like purchasing products, finding information, and connecting with others. As the internet a tool universally used in commerce throughout business practices, it is important to know what sites you are accessing, as well as any threats that may exist from those domains.
With phishing attempts and other cyber threats on the rise, online security should be a key part of business planning. Preparing for these risks will help prevent the theft of information and protect your organization and client-base. Having a plan in place will lend your business credibility and reliability in the eyes of your users and partners, who will know that they can trust you with their sensitive data.
The following APIs will allow you to fully validate a domain name through several avenues and checks. The first three functions include a basic validation of the domain name’s existence, whether it is syntactically valid, and whether the endpoint pass virus scan checks. The last two functions will return important information about a domain including its quality score and WHOIS registration data.
Before you can run any of these functions, however, you will first need to install the SDK library with Maven or Gradle. To install with Maven, add a Jitpack reference to the repository in pom.xml:
Then, you can add a reference to the dependency:
To install with Gradle, add the reference to your root build.gradle at the end of repositories:
Then, you can add the dependency in build.gradle:
The first function below is aimed at performing a simple domain name check that will detect whether a domain name is valid. This is done through contacting DNS services to perform a live validation. This ensures that the response is always current and accurate. By performing this action, you can instantly find out if the input domain name is correct.
After installing the SDK, you can add the imports to the top of the file and call the function:
This will return whether the domain is valid and that it exists. To ensure that this API works properly, you need to ensure certain requirements are met:
- The domain name is input correctly, enclosed in double-quotes (i.e., “Cloudmersive.com”).
- You have input your API Key. This can be retrieved at no cost on the Cloudmersive website, providing 800 monthly calls across our API library.
If you would like to perform a more in-depth scan of a domain name, the following function will fully validate a URL by checking if it is syntactically valid, whether it exists, and whether the endpoints pass virus scan checks. Like the previous function, install the SDK first and add the imports to the top of the file. Then, you can call the function:
This will return all the responses mentioned above, as well as a well-formed URL output for your use.
To narrow the scope of the URL validation slightly, the next API will only check if the URL is syntactically valid. This is ideal for those who want to ensure any input URLs are formatted correctly before storage or use. Install the SDK and add the imports to the file—then, call the function:
This will return a response stating whether the URL was valid, as well as the well-formed URL.
After performing your validation function, you can also check for information from the domain name including the quality score and WHOIS information. The next function is meant to validate the quality of a domain name with support for over 9 million names. With a score range of 0.0 to 10.0, higher scores indicate higher levels of trust and authority in the domain name. After installing the SDK, call the function as shown below:
Finally, we can check for a domain name’s WHOIS information, which details registrant information. This is useful for finding information on the owner of a web page, either to discover new leads or to help mitigate threats from dangerous sites. You can run the API by calling the function below:
This will return a domain validation as well as registrant name, organization, email, street address, raw address, telephone number, the WHOIS server, the raw text record, and the created date.
With all of these functions, you can ensure that you are informed and protected when accessing new links and websites.
Opinions expressed by DZone contributors are their own.