How can I use an API to detect if an IP address is a VPN in Java?

To detect if an IP address is a VPN using IP2Location.io in Java, you can use the IP2Location.io API to retrieve geolocation and related data.

  1. We'll assume that you have already installed Java and we won't cover that here. You will also need a paid subscription to the Security plan. Subscribe now!
  2. We'll need to download the gson jar file from Maven. Go to Maven. Then, click on the latest version. Next, click on the "Downloads" link and click on "jar" to download the jar file. Save that jar file into the same folder as your test code.
  3. Save the below code into a file called Main.java on your computer.
    Java
    
    import java.net.URI;
    import java.net.http.HttpClient;
    import java.net.http.HttpRequest;
    import java.net.http.HttpResponse;
    import java.util.concurrent.CompletableFuture;
    
    import com.google.gson.*;
    
    public class Main {
        public Main() {}
        public static void main(String[] args) {
            try {
                String MyKey = "YOUR_API_KEY";
                String MyIP = "8.8.8.8";
    
                String url = "https://api.ip2location.io?format=json&key=" + MyKey + "&ip=" + MyIP;
                HttpRequest request = HttpRequest.newBuilder()
                    .uri(new URI(url))
                    .GET()
                    .build();
                HttpClient client = HttpClient.newHttpClient();
                CompletableFuture < HttpResponse < String >> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
                int statusCode = response.thenApply(HttpResponse::statusCode).get();
                if (statusCode == 200) {
                    String rawJSON = response.thenApply(HttpResponse::body).get();
                    JsonObject MyObj = JsonParser.parseString(rawJSON).getAsJsonObject();
                    if (MyObj.has("proxy")) {
                        if (MyObj.getAsJsonObject("proxy").get("is_vpn").getAsBoolean()) {
                            System.out.println("The IP " + MyIP + " is a VPN.");
                        } else {
                            System.out.println("The IP " + MyIP + " is NOT a VPN.");
                        }
                    } else {
                        System.out.println("ERROR: The is_vpn field requires a paid subscription to the Security plan.");
                    }
                } else if (statusCode == 400 || statusCode == 401) {
                    String rawJSON = response.thenApply(HttpResponse::body).get();
                    if (rawJSON.contains("error_message")) {
                        throw new Exception("ERROR: " + JsonParser.parseString(rawJSON).getAsJsonObject().getAsJsonObject("error").get("error_message").getAsString());
                    }
                    throw new Exception(rawJSON);
                } else {
                    String err = response.thenApply(HttpResponse::body).get();
                    throw new Exception(err);
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
    			
  4. In the command line, run the below commands to compile and run. Modify the commands for your version of the gson jar.
    Bash
    
    javac -classpath gson-2.11.0.jar Main.java
    java -cp gson-2.11.0.jar; Main
    			

This script will check if the specified IP address is a VPN. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.

Other Languages

Unlock Location Insights For Free

Empower your applications with accurate IP geolocation information now.

Try It for Free