Fully Validate a URL in Java
Learn how to check URL validity across several important criteria in one step.
Hey everyone!
A URL can be invalid for several different reasons, and it isn’t all that straightforward to check for every potential error in one go. If you’re finding yourself struggling to identify easy, efficient, and comprehensive methods for validating URL strings in your Java applications, you’re not alone: most open-source solutions fail to combine syntax, domain, and endpoint validity checks in one go.
Thankfully, there’s an API for that!
Our Full URL Validation API validates URL strings across all three of the above categories, and it also returns a well-formed version of our original URL string in the API response.
In this tutorial, we’ll walk through how to structure our Full URL Validation API call step by step using Java code examples. This will make it easy for us to validate important URL inputs in any of our Java applications.
Step 1: Install the Client SDK
The first step in our process is client SDK installation. We can install a Maven or Gradle SDK depending on which build tool we’re using; below, I’ve included examples to get started with both.
Install with Maven
To install the SDK with Maven, we can first add the following reference to our pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>Next, we can add a reference to our pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>Install with Gradle
To install with Gradle, we can add the below code to the root.build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}And then we can add the dependency in build.gradle:
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v4.25'
}Step 2: Add the Imports
In our next step, we’ll add the following imports to our controller:
// 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;Step 3: API Key Configuration
In our third step, we’ll turn our attention to authorizing our API call with a Cloudmersive API key. Let’s first copy the configuration snippet below:
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");After that, let’s head to the Cloudmersive website and set up a free account. That’ll give us a free Cloudmersive API key with a limit of 800 API calls per month and zero additional commitments (once we reach our API call total, it’ll simply reset the following month). We can paste our API key in the appropriate line, and we’re all set.
Step 4: Create an Instance of the API
In our final step, we’re going to instance the API and call the Full URL Validation function. Calling the function with our URL string input will 1) check if our URL string syntax is correct, 2) check if the domain exists (via DNS lookup), 3) check if the endpoint is up and passes virus scan checks, and 4) return a well-formed version of our input URL string.
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();
}Our URL input request can follow the below example JSON model:
{
"URL": "string"
}And we can expect our validation response to follow below example structure:
{
"ValidURL": true,
"Valid_Syntax": true,
"Valid_Domain": true,
"Valid_Endpoint": true,
"WellFormedURL": "string"
}All done! Now we have all the code we’ll need to quickly validate URL inputs in our Java application.
Have any questions for us after this demonstration? We’d love to hear from you! Feel free to contact us any time.


