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 <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    const wchar_t *url;
    const wchar_t *jsonResp;
    HCkJsonObjectW json;
    int Status;
    BOOL TC;
    BOOL RD;
    BOOL RA;
    BOOL AD;
    BOOL CD;
    int i;
    int count_i;
    const wchar_t *name;
    int type;
    int TTL;
    const wchar_t *data;
    const wchar_t *ipAddr;

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

    http = CkHttpW_Create();

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

    CkHttpW_putAccept(http,L"application/dns-json");

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

    jsonResp = CkHttpW_quickGetStr(http,url);
    if (CkHttpW_getLastMethodSuccess(http) != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        return;
    }

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

    json = CkJsonObjectW_Create();
    CkJsonObjectW_Load(json,jsonResp);
    CkJsonObjectW_putEmitCompact(json,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

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

    Status = CkJsonObjectW_IntOf(json,L"Status");
    TC = CkJsonObjectW_BoolOf(json,L"TC");
    RD = CkJsonObjectW_BoolOf(json,L"RD");
    RA = CkJsonObjectW_BoolOf(json,L"RA");
    AD = CkJsonObjectW_BoolOf(json,L"AD");
    CD = CkJsonObjectW_BoolOf(json,L"CD");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"Question");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        name = CkJsonObjectW_stringOf(json,L"Question[i].name");
        type = CkJsonObjectW_IntOf(json,L"Question[i].type");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"Answer");
    // The domain name resolves to 1 or more IP addresses..
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        name = CkJsonObjectW_stringOf(json,L"Answer[i].name");
        type = CkJsonObjectW_IntOf(json,L"Answer[i].type");
        TTL = CkJsonObjectW_IntOf(json,L"Answer[i].TTL");
        ipAddr = CkJsonObjectW_stringOf(json,L"Answer[i].data");

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



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);

    }