Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples
Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) Example: Http.SetCookieXml method

Demonstrates the SetCookieXml method.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    EXEC sp_OASetProperty @http, 'SaveCookies', 1
    EXEC sp_OASetProperty @http, 'CookieDir', 'c:/temp/cookie_cache'
    -- For this initial request, we are not sending cookies.
    -- We are starting a new (cookie) session and saving the cookies we receive.
    EXEC sp_OASetProperty @http, 'SendCookies', 0

    -- Do a request that establishes and saves cookies in files in the cookie directory.
    DECLARE @html nvarchar(4000)
    EXEC sp_OAMethod @http, 'QuickGetStr', @html OUT, 'https://google.com/'

    -- Examine the LastResponseHeader to see the cookies that were received:
    EXEC sp_OAGetProperty @http, 'LastResponseHeader', @sTmp0 OUT
    PRINT @sTmp0

    PRINT '----------------------------------'

    -- We can see the following Set-Cookie response header fields:

    -- Set-Cookie: AEC=AVh_V2h-fsL2-****; expires=Mon, 23-Feb-2026 19:15:10 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
    -- Set-Cookie: NID=525=XC_cL****Hn7-WONX; expires=Thu, 26-Feb-2026 19:15:10 GMT; path=/; domain=.google.com; HttpOnly

    -- The following file was created:  c:/temp/cookie_cache/google_com.xml

    -- Examine the cookies received in the above request.
    EXEC sp_OAMethod @http, 'GetCookieXml', @sTmp0 OUT, 'google.com'
    PRINT @sTmp0

    PRINT '----------------------------------'

    -- Sample output.

    -- <?xml version="1.0" encoding="utf-8"?>
    -- <cookies>
    --     <cookie key=".google.com,/,AEC" v="0" expire="Mon, 23-Feb-2026 19:05:20 GMT" secure="yes">
    --         <AEC>AVh_V2gL8QzTdFGYq6_rS6ktBfqm8WNG3pzHxS2nTZD5i23dRBau2c4ZRA</AEC>
    --     </cookie>
    --     <cookie key=".google.com,/,NID" v="0" expire="Thu, 26-Feb-2026 19:05:20 GMT">
    --         <NID>525=SuLcnaSkFqF4Jz_jLEq4kt_f3MY2Xro1VDoVzLKvp8XHcW2UHuLKJSr55iDeW0NiRIPXoAwJWF1-YNl29unX2xfhEWsS5BhbuK_2DXdD9cTOmn5BSENMhZasxeJ71mEP2PQRMXBndqnl41DhblC2jjdac_so4TESIll1B0GCVe9wRFjqI6DTZItRCj61BHmr1_RAQi0_jrh_ihn6KYtIFEY7</NID>
    --     </cookie>
    -- </cookies>

    -- -------------------------------------------------------------------------------------------
    -- Let's say we want to continue with the session at some later time with a new HTTP object.
    -- One way to do it is to simply set the same cookie properties:
    DECLARE @httpB int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @httpB OUT

    EXEC sp_OASetProperty @httpB, 'SaveCookies', 1
    EXEC sp_OASetProperty @httpB, 'CookieDir', 'c:/temp/cookie_cache'
    EXEC sp_OASetProperty @httpB, 'SendCookies', 1

    -- The cookies from the cache are sent.
    EXEC sp_OAMethod @httpB, 'QuickGetStr', @html OUT, 'https://google.com/'

    -- Examine the LastHeader to see the contents of the Cookie header field.
    EXEC sp_OAGetProperty @httpB, 'LastHeader', @sTmp0 OUT
    PRINT @sTmp0

    PRINT '----------------------------------'

    -- -------------------------------------------------------------------------------------------
    -- Another way to do it is to explicitly load the cookies from XML
    -- and set the cookies for the particular domain.

    DECLARE @httpC int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @httpC OUT

    EXEC sp_OASetProperty @httpC, 'SaveCookies', 1
    -- Do not save cookies to files, just keep them in memory.
    EXEC sp_OASetProperty @httpC, 'CookieDir', 'memory'
    EXEC sp_OASetProperty @httpC, 'SendCookies', 1

    DECLARE @sbCookiesXml int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbCookiesXml OUT

    EXEC sp_OAMethod @sbCookiesXml, 'LoadFile', @success OUT, 'c:/temp/cookie_cache/google_com.xml', 'utf-8'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sbCookiesXml, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @httpB
        EXEC @hr = sp_OADestroy @httpC
        EXEC @hr = sp_OADestroy @sbCookiesXml
        RETURN
      END

    EXEC sp_OAMethod @sbCookiesXml, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @httpC, 'SetCookieXml', @success OUT, 'google.com', @sTmp0
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @httpC, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @httpB
        EXEC @hr = sp_OADestroy @httpC
        EXEC @hr = sp_OADestroy @sbCookiesXml
        RETURN
      END

    EXEC sp_OAMethod @httpC, 'QuickGetStr', @html OUT, 'https://google.com/'

    -- Examine the LastHeader to see the contents of the Cookie header field.
    EXEC sp_OAGetProperty @httpC, 'LastHeader', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @httpB
    EXEC @hr = sp_OADestroy @httpC
    EXEC @hr = sp_OADestroy @sbCookiesXml


END
GO

 

© 2000-2025 Chilkat Software, Inc. All Rights Reserved.