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 FreeA 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:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, you can add a reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
To install with Gradle, add the reference to your root build.gradle at the end of repositories:
xxxxxxxxxx
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then, you can add the dependency in build.gradle:
xxxxxxxxxx
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
}
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.DomainApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
DomainApi apiInstance = new DomainApi();
String domain = "domain_example"; // String | Domain name to check, for example \"cloudmersive.com\". The input is a string so be sure to enclose it in double-quotes.
try {
CheckResponse result = apiInstance.domainCheck(domain);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling DomainApi#domainCheck");
e.printStackTrace();
}
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.DomainApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
DomainApi apiInstance = new DomainApi();
ValidateUrlRequestFull request = new ValidateUrlRequestFull(); // ValidateUrlRequestFull | Input URL request
try {
ValidateUrlResponseFull result = apiInstance.domainUrlFull(request);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling DomainApi#domainUrlFull");
e.printStackTrace();
}
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.DomainApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
DomainApi apiInstance = new DomainApi();
ValidateUrlRequestSyntaxOnly request = new ValidateUrlRequestSyntaxOnly(); // ValidateUrlRequestSyntaxOnly | Input URL information
try {
ValidateUrlResponseSyntaxOnly result = apiInstance.domainUrlSyntaxOnly(request);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling DomainApi#domainUrlSyntaxOnly");
e.printStackTrace();
}
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.DomainApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
DomainApi apiInstance = new DomainApi();
String domain = "domain_example"; // String | Domain name to check, for example \"cloudmersive.com\".
try {
DomainQualityResponse result = apiInstance.domainQualityScore(domain);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling DomainApi#domainQualityScore");
e.printStackTrace();
}
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.DomainApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
DomainApi apiInstance = new DomainApi();
String domain = "domain_example"; // String | Domain name to check, for example \"cloudmersive.com\". The input is a string so be sure to enclose it in double-quotes.
try {
WhoisResponse result = apiInstance.domainPost(domain);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling DomainApi#domainPost");
e.printStackTrace();
}
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.
Comments