Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttpW http;

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

    http.put_Accept(L"application/dns-json");

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

    const wchar_t *jsonResp = http.quickGetStr(url);
    if (http.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    wprintf(L"Response Status Code: %d\n",http.get_LastStatus());

    CkJsonObjectW json;
    json.Load(jsonResp);
    json.put_EmitCompact(false);
    wprintf(L"%s\n",json.emit());

    if (http.get_LastStatus() != 200) {
        wprintf(L"Failed.\n");
        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

    int Status;
    bool TC;
    bool RD;
    bool RA;
    bool AD;
    bool CD;
    int i;
    int count_i;
    const wchar_t *name = 0;
    int type;
    int TTL;
    const wchar_t *data = 0;
    const wchar_t *ipAddr = 0;

    Status = json.IntOf(L"Status");
    TC = json.BoolOf(L"TC");
    RD = json.BoolOf(L"RD");
    RA = json.BoolOf(L"RA");
    AD = json.BoolOf(L"AD");
    CD = json.BoolOf(L"CD");
    i = 0;
    count_i = json.SizeOfArray(L"Question");
    while (i < count_i) {
        json.put_I(i);
        name = json.stringOf(L"Question[i].name");
        type = json.IntOf(L"Question[i].type");
        i = i + 1;
    }

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

        wprintf(L"IP Address: %s\n",ipAddr);
        i = i + 1;
    }
    }