DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Trending

  • Building Production-Grade GenAI on GCP with Vertex AI Agent Builder
  • Leveraging Apache Flink Dashboard for Real-Time Data Processing in AWS Apache Flink Managed Service
  • From APIs to Actions: Rethinking Back-End Design for Agents
  • Tactical Domain-Driven Design: Bringing Strategy to Code
  1. DZone
  2. Coding
  3. Java
  4. How to Validate a Domain Name in Java

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.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Dec. 17, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
9.6K Views

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:

XML
 




x


 
1
<repositories>
2
    <repository>
3
        <id>jitpack.io</id>
4
        <url>https://jitpack.io</url>
5
    </repository>
6
</repositories>



Then, you can add a reference to the dependency:

XML
 




xxxxxxxxxx
1


 
1
<dependencies>
2
<dependency>
3
    <groupId>com.github.Cloudmersive</groupId>
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
5
    <version>v3.54</version>
6
</dependency>
7
</dependencies>



To install with Gradle, add the reference to your root build.gradle at the end of repositories:

Java
 




xxxxxxxxxx
1


 
1
allprojects {
2
    repositories {
3
        ...
4
        maven { url 'https://jitpack.io' }
5
    }
6
}



Then, you can add the dependency in build.gradle:

Java
 




xxxxxxxxxx
1


 
1
dependencies {
2
        implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
3
}



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:

Java
 




xxxxxxxxxx
1
25


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.DomainApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
DomainApi apiInstance = new DomainApi();
17
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.
18
try {
19
    CheckResponse result = apiInstance.domainCheck(domain);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling DomainApi#domainCheck");
23
    e.printStackTrace();
24
}



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:

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.DomainApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
DomainApi apiInstance = new DomainApi();
17
ValidateUrlRequestFull request = new ValidateUrlRequestFull(); // ValidateUrlRequestFull | Input URL request
18
try {
19
    ValidateUrlResponseFull result = apiInstance.domainUrlFull(request);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling DomainApi#domainUrlFull");
23
    e.printStackTrace();
24
}



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:

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.DomainApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
DomainApi apiInstance = new DomainApi();
17
ValidateUrlRequestSyntaxOnly request = new ValidateUrlRequestSyntaxOnly(); // ValidateUrlRequestSyntaxOnly | Input URL information
18
try {
19
    ValidateUrlResponseSyntaxOnly result = apiInstance.domainUrlSyntaxOnly(request);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling DomainApi#domainUrlSyntaxOnly");
23
    e.printStackTrace();
24
}



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:

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.DomainApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
DomainApi apiInstance = new DomainApi();
17
String domain = "domain_example"; // String | Domain name to check, for example \"cloudmersive.com\".
18
try {
19
    DomainQualityResponse result = apiInstance.domainQualityScore(domain);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling DomainApi#domainQualityScore");
23
    e.printStackTrace();
24
}



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:

Java
 




xxxxxxxxxx
1
24


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.DomainApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
DomainApi apiInstance = new DomainApi();
17
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.
18
try {
19
    WhoisResponse result = apiInstance.domainPost(domain);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling DomainApi#domainPost");
23
    e.printStackTrace();
24
}



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. 

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook