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

Look up an IP country with MaxMind GeoIP

See more Geolocation Examples

Call the GeoIP Country web service using HTTP Basic authentication. Set the username to your numeric MaxMind account ID and the password to your license key. The account needs access to the web service, such as a trial or service credit. Both IPv4 and IPv6 are supported.

country describes the geolocated country. registered_country describes where the network is registered. The Country service returns country-level data. City-level lookups use the separately entitled /geoip/v2.1/city/ service. A GeoLite web-service account uses geolite.info as its host.

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 CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    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";
    CkHttp http = new CkHttp();
    http.put_RequireSslCertVerify(true);
    http.put_ConnectTimeout(10);
    http.put_ReadTimeout(30);
    http.put_Accept("application/json");
    http.put_BasicAuth(true);
    http.put_Login("MAXMIND_ACCOUNT_ID");
    http.put_Password("MAXMIND_LICENSE_KEY");
    CkStringBuilder url = new CkStringBuilder();
    url.Append("https://geoip.maxmind.com/geoip/v2.1/country/");
    url.Append(http.urlEncode(ipAddress));
    CkHttpResponse response = new CkHttpResponse();
    success = http.HttpNoBody("GET",url.getAsString(),response);
    if (response.get_StatusCode() == 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;
        }

    CkJsonObject json = new CkJsonObject();
    boolean parsed = json.Load(response.bodyStr());
    if ((success != true) || (response.get_StatusCode() < 200) || (response.get_StatusCode() >= 300)) {
        System.out.println("Lookup failed. HTTP " + response.get_StatusCode());
        if (parsed == true) {
            if (json.JsonTypeOf("code") == 1) {
                System.out.println(json.stringOf("code"));
                }

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

            }

        return;
        }

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

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

    System.out.println("IP address: " + json.stringOf("traits.ip_address"));
    if ((json.JsonTypeOf("country.names.en") == 1) && (json.StringOfEquals("country.names.en","",true) != true)) {
        System.out.println("Country: " + json.stringOf("country.names.en"));
        }
    else {
        System.out.println("Country: unavailable");
        }

    if ((json.JsonTypeOf("country.iso_code") == 1) && (json.StringOfEquals("country.iso_code","",true) != true)) {
        System.out.println("Country code: " + json.stringOf("country.iso_code"));
        }
    else {
        System.out.println("Country code: unavailable");
        }

    if ((json.JsonTypeOf("continent.names.en") == 1) && (json.StringOfEquals("continent.names.en","",true) != true)) {
        System.out.println("Continent: " + json.stringOf("continent.names.en"));
        }
    else {
        System.out.println("Continent: unavailable");
        }

    if ((json.JsonTypeOf("registered_country.iso_code") == 1) && (json.StringOfEquals("registered_country.iso_code","",true) != true)) {
        System.out.println("Registered country code: " + json.stringOf("registered_country.iso_code"));
        }
    else {
        System.out.println("Registered country code: unavailable");
        }
  }
}