Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

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

    EXEC sp_OASetProperty @http, 'Accept', 'application/dns-json'

    DECLARE @url nvarchar(4000)
    SELECT @url = 'https://cloudflare-dns.com/dns-query?name=chilkat.io&type=A'

    DECLARE @jsonResp nvarchar(4000)
    EXEC sp_OAMethod @http, 'QuickGetStr', @jsonResp OUT, @url
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        RETURN
      END


    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    PRINT 'Response Status Code: ' + @iTmp0

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    DECLARE @success int
    EXEC sp_OAMethod @json, 'Load', @success OUT, @jsonResp
    EXEC sp_OASetProperty @json, 'EmitCompact', 0
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

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

    DECLARE @Status int

    DECLARE @TC int

    DECLARE @RD int

    DECLARE @RA int

    DECLARE @AD int

    DECLARE @CD int

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @name nvarchar(4000)

    DECLARE @type int

    DECLARE @TTL int

    DECLARE @data nvarchar(4000)

    DECLARE @ipAddr nvarchar(4000)

    EXEC sp_OAMethod @json, 'IntOf', @Status OUT, 'Status'
    EXEC sp_OAMethod @json, 'BoolOf', @TC OUT, 'TC'
    EXEC sp_OAMethod @json, 'BoolOf', @RD OUT, 'RD'
    EXEC sp_OAMethod @json, 'BoolOf', @RA OUT, 'RA'
    EXEC sp_OAMethod @json, 'BoolOf', @AD OUT, 'AD'
    EXEC sp_OAMethod @json, 'BoolOf', @CD OUT, 'CD'
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'Question'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'Question[i].name'
        EXEC sp_OAMethod @json, 'IntOf', @type OUT, 'Question[i].type'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'Answer'
    -- The domain name resolves to 1 or more IP addresses..
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'Answer[i].name'
        EXEC sp_OAMethod @json, 'IntOf', @type OUT, 'Answer[i].type'
        EXEC sp_OAMethod @json, 'IntOf', @TTL OUT, 'Answer[i].TTL'
        EXEC sp_OAMethod @json, 'StringOf', @ipAddr OUT, 'Answer[i].data'


        PRINT 'IP Address: ' + @ipAddr
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO