Sample code for 30+ languages & platforms
Java

Cloudfare DNS over HTTPS

See more Cloudfare Examples

Cloudflare offers a DNS over HTTPS resolver at: https://cloudflare-dns.com/dns-query

This example demonstrates how to do a DNS lookup for a domain using Cloudfare's HTTPS resolver.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Send the following CURL request:   curl -H 'accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=chilkat.io&type=A'

        http.setAccept("application/dns-json");

        String url = "https://cloudflare-dns.com/dns-query?name=chilkat.io&type=A";

        String jsonResp;
        try {
            jsonResp = http.quickGetStr(url);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Response Status Code: " + http.getLastStatus());

        JsonObject json = new JsonObject();
        json.load(jsonResp);
        json.setEmitCompact(false);
        System.out.println(json.emit());

        if (http.getLastStatus() != 200) {
            System.out.println("Failed.");
            return;
        }

        // Sample output...
        // (See the parsing code below..)
        //

        // {
        //   "Status": 0,
        //   "TC": false,
        //   "RD": true,
        //   "RA": true,
        //   "AD": false,
        //   "CD": false,
        //   "Question": [
        //     {
        //       "name": "chilkat.io.",
        //       "type": 1
        //     }
        //   ],
        //   "Answer": [
        //     {
        //       "name": "chilkat.io.",
        //       "type": 1,
        //       "TTL": 900,
        //       "data": "52.25.83.238"
        //     }
        //   ]
        // }

        // Use this online tool to generate parsing code from sample JSON:
        // Generate Parsing Code from JSON

        String name = "";
        int type = 0;
        int TTL = 0;
        String ipAddr = "";

        int Status = json.intOf("Status");
        boolean TC = json.boolOf("TC");
        boolean RD = json.boolOf("RD");
        boolean RA = json.boolOf("RA");
        boolean AD = json.boolOf("AD");
        boolean CD = json.boolOf("CD");
        int i = 0;
        int count_i = json.sizeOfArray("Question");
        while (i < count_i) {
            json.setI(i);
            name = json.stringOf("Question[i].name");
            type = json.intOf("Question[i].type");
            i = i + 1;
        }

        i = 0;
        count_i = json.sizeOfArray("Answer");
        // The domain name resolves to 1 or more IP addresses..
        while (i < count_i) {
            json.setI(i);
            name = json.stringOf("Answer[i].name");
            type = json.intOf("Answer[i].type");
            TTL = json.intOf("Answer[i].TTL");
            ipAddr = json.stringOf("Answer[i].data");

            System.out.println("IP Address: " + ipAddr);
            i = i + 1;
        }
    }
}