Site icon NgDeveloper

Domain availability Java program

Requirement:
Register in the Freedomainapi.com for api key, of course its free.
Once you got the apikey replace in the below program and run.

Requied Jar file:
Java Json Jar

Domain Availability Java program:

[java]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.json.JSONException;
import org.json.JSONObject;

public class DomainCheck {

public static void main(String[] args) {
DomainCheck dc = new DomainCheck();
String apikey = "XXXXXXXXXX";
String isEnteredDomainAvailable = dc.getDomainAvailable("fforfriends.com",apikey);

System.out.println(isEnteredDomainAvailable);

}

private String getDomainAvailable(String domainName,String apiKey) {
String domainAvailabilityStatus = null;

try {
URL url = new URL(
"http://freedomainapi.com/?key="+apiKey+"&domain="+domainName+"");
URLConnection connection = url.openConnection();
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
while ((line = reader.readLine()) != null) {
builder.append(line);
}

try {
JSONObject json = new JSONObject(builder.toString());
String isDomainValid = (String) json.get("status");
if (isDomainValid.equalsIgnoreCase("success")) {
boolean isDomainAvailableJson = (boolean) json.get("available");
if(isDomainAvailableJson){
domainAvailabilityStatus = "congratz, Domain Available for you!";
}else{
domainAvailabilityStatus = "Someone using the Domain already";
}
} else {
domainAvailabilityStatus = "API key wrong (or) Invalid Domain Name" ;
}

} catch (JSONException e) {
domainAvailabilityStatus = "Server May Be Busy or Server May Be Down";
}
} catch (IOException e) {
domainAvailabilityStatus = "Server May Be Busy or Server May Be Down";
}
return domainAvailabilityStatus;
}
}
[/java]

Output (once apikey changed):
congratz, Domain Available for you!

Revert me back if you need further assistance.

Exit mobile version