Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>

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

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

    http = CkHttp_Create();

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

    CkHttp_putAccept(http,"application/dns-json");

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

    jsonResp = CkHttp_quickGetStr(http,url);
    if (CkHttp_getLastMethodSuccess(http) != TRUE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        return;
    }

    printf("Response Status Code: %d\n",CkHttp_getLastStatus(http));

    json = CkJsonObject_Create();
    CkJsonObject_Load(json,jsonResp);
    CkJsonObject_putEmitCompact(json,FALSE);
    printf("%s\n",CkJsonObject_emit(json));

    if (CkHttp_getLastStatus(http) != 200) {
        printf("Failed.\n");
        CkHttp_Dispose(http);
        CkJsonObject_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 = CkJsonObject_IntOf(json,"Status");
    TC = CkJsonObject_BoolOf(json,"TC");
    RD = CkJsonObject_BoolOf(json,"RD");
    RA = CkJsonObject_BoolOf(json,"RA");
    AD = CkJsonObject_BoolOf(json,"AD");
    CD = CkJsonObject_BoolOf(json,"CD");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(json,"Question");
    while (i < count_i) {
        CkJsonObject_putI(json,i);
        name = CkJsonObject_stringOf(json,"Question[i].name");
        type = CkJsonObject_IntOf(json,"Question[i].type");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObject_SizeOfArray(json,"Answer");
    // The domain name resolves to 1 or more IP addresses..
    while (i < count_i) {
        CkJsonObject_putI(json,i);
        name = CkJsonObject_stringOf(json,"Answer[i].name");
        type = CkJsonObject_IntOf(json,"Answer[i].type");
        TTL = CkJsonObject_IntOf(json,"Answer[i].TTL");
        ipAddr = CkJsonObject_stringOf(json,"Answer[i].data");

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



    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);

    }