Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Geolocation of IP Address

See more REST Examples

Retrieve country, state or province, and city for a public IP address using the DB-IP v2 free JSON API. This replaces the retired freegeoip.net request and the historical two-service comparison. The sample sends one HTTPS request, checks transport and service errors, and reads the returned fields.

DB-IP documents 500 daily requests for its free service. Its free HTTPS endpoint returned a successful lookup during verification on November 20, 2026. For other providers and authenticated services, see the Geolocation examples.

IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.

Service documentation

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        // Use a public IP address. Provider results can change as their databases are updated.
        String ipAddress = "8.8.8.8";
        Http http = new Http();
        http.setRequireSslCertVerify(true);
        http.setConnectTimeout(10);
        http.setReadTimeout(30);
        http.setAccept("application/json");
        StringBuilder url = new StringBuilder();
        url.append("https://api.db-ip.com/v2/free/");
        url.append(http.urlEncode(ipAddress));
        HttpResponse response = new HttpResponse();
        try {
            http.httpNoBody("GET", url.getAsString(), response);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (response.getStatusCode() == 0) {
            // Request diagnostics can contain an API key. Report transport failure without logging credentials.
            System.out.println("No HTTP response. Check connectivity, DNS, and TLS configuration.");
            return;
        }

        JsonObject json = new JsonObject();
        boolean parsed;
        try {
            json.load(response.getBodyStr());
            parsed = true;
        } catch (ChilkatException e) {
            parsed = false;
        }
        if ((!success) || ((response.getStatusCode() < 200) || (response.getStatusCode() >= 300))) {
            System.out.println("Lookup failed. HTTP " + response.getStatusCode());
            if (parsed) {
                if (json.jsonTypeOf("errorCode") == 1) {
                    System.out.println(json.stringOf("errorCode"));
                }

                if (json.jsonTypeOf("error") == 1) {
                    System.out.println(json.stringOf("error"));
                }
            }

            return;
        }

        if (!parsed) {
            System.out.println("Expected a JSON object in the lookup response.");
            return;
        }

        if ((json.hasMember("errorCode")) || (json.jsonTypeOf("error") == 1)) {
            System.out.println("The service could not complete the lookup.");
            if (json.jsonTypeOf("errorCode") == 1) {
                System.out.println(json.stringOf("errorCode"));
            }

            if (json.jsonTypeOf("error") == 1) {
                System.out.println(json.stringOf("error"));
            }

            return;
        }

        if ((json.jsonTypeOf("ipAddress") != 1) || (json.stringOfEquals("ipAddress", "", true))) {
            System.out.println("The response did not contain the expected IP address.");
            return;
        }

        System.out.println("IP address: " + json.stringOf("ipAddress"));
        if ((json.jsonTypeOf("countryName") == 1) && (!json.stringOfEquals("countryName", "", true))) {
            System.out.println("Country: " + json.stringOf("countryName"));
        } else {
            System.out.println("Country: unavailable");
        }

        if ((json.jsonTypeOf("countryCode") == 1) && (!json.stringOfEquals("countryCode", "", true))) {
            System.out.println("Country code: " + json.stringOf("countryCode"));
        } else {
            System.out.println("Country code: unavailable");
        }

        if ((json.jsonTypeOf("stateProv") == 1) && (!json.stringOfEquals("stateProv", "", true))) {
            System.out.println("State or province: " + json.stringOf("stateProv"));
        } else {
            System.out.println("State or province: unavailable");
        }

        if ((json.jsonTypeOf("city") == 1) && (!json.stringOfEquals("city", "", true))) {
            System.out.println("City: " + json.stringOf("city"));
        } else {
            System.out.println("City: unavailable");
        }
    }
}