Sample code for 30+ languages & platforms
SQL Server

Demonstrates how to Get Cookies Sent by the HTTP Server

See more HTTP Examples

This example demonstrates how to get the cookies that have been received from the HTTP server.

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
    -- This example assumes the Chilkat HTTP 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

    -- When the HTTP object's SaveCookies property is enabled, cookies received from the 
    -- server are saved either to the directory specified by CookieDir (where they are stored as XML files) 
    -- or in memory if "memory" is specified.

    EXEC sp_OASetProperty @http, 'SaveCookies', 1
    EXEC sp_OASetProperty @http, 'CookieDir', 'memory'

    -- Do a few GET's 
    DECLARE @html nvarchar(4000)
    EXEC sp_OAMethod @http, 'QuickGetStr', @html OUT, 'https://google.com/'
    EXEC sp_OAMethod @http, 'QuickGetStr', @html OUT, 'https://stripe.com/'

    -- Examine the cookies received from each domain.
    DECLARE @xmlStr nvarchar(4000)
    EXEC sp_OAMethod @http, 'GetCookieXml', @xmlStr OUT, 'google.com'

    PRINT @xmlStr

    -- Sample output:

    -- <?xml version="1.0" encoding="utf-8"?>
    -- <cookies>
    --     <cookie key=".google.com,/,AEC" v="0" expire="Tue, 17-Feb-2026 11:32:20 GMT" secure="yes">
    --         <AEC>AVh_......</AEC>
    --     </cookie>
    --     <cookie key=".google.com,/,NID" v="0" expire="Fri, 20-Feb-2026 11:32:20 GMT">
    --         <NID>525=A57i.......</NID>
    --     </cookie>
    -- </cookies>

    EXEC sp_OAMethod @http, 'GetCookieXml', @xmlStr OUT, 'stripe.com'

    PRINT @xmlStr

    -- Sample output:

    -- <?xml version="1.0" encoding="utf-8"?>
    -- <cookies>
    --     <cookie key="stripe.com,/,cid" v="0" maxAge="7776000" secure="yes">
    --         <cid>eba2891e-89e4-4aec-b9cf-e799d5b4b230</cid>
    --     </cookie>
    -- </cookies>

    EXEC @hr = sp_OADestroy @http


END
GO