Sample code for 30+ languages & platforms
DataFlex

Example: Http.SetCookieXml method

Demonstrates the SetCookieXml method.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    String sHtml
    Handle hoHttpB
C    Handle hoHttpC
    Handle hoSbCookiesXml
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Set ComSaveCookies Of hoHttp To True
    Set ComCookieDir Of hoHttp To "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.
    Set ComSendCookies Of hoHttp To False

    // Do a request that establishes and saves cookies in files in the cookie directory.
    Get ComQuickGetStr Of hoHttp "https://google.com/" To sHtml

    // Examine the LastResponseHeader to see the cookies that were received:
    Get ComLastResponseHeader Of hoHttp To sTemp1
    Showln sTemp1
    Showln "----------------------------------"

    // 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.
    Get ComGetCookieXml Of hoHttp "google.com" To sTemp1
    Showln sTemp1
    Showln "----------------------------------"

    // 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:
    Get Create (RefClass(cComChilkatHttp)) To hoHttpB
    If (Not(IsComObjectCreated(hoHttpB))) Begin
        Send CreateComObject of hoHttpB
    End

    Set ComSaveCookies Of hoHttpB To True
    Set ComCookieDir Of hoHttpB To "c:/temp/cookie_cache"
    Set ComSendCookies Of hoHttpB To True

    // The cookies from the cache are sent.
    Get ComQuickGetStr Of hoHttpB "https://google.com/" To sHtml

    // Examine the LastHeader to see the contents of the Cookie header field.
    Get ComLastHeader Of hoHttpB To sTemp1
    Showln sTemp1
    Showln "----------------------------------"

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttpC
    If (Not(IsComObjectCreated(hoHttpC))) Begin
        Send CreateComObject of hoHttpC
    End

    Set ComSaveCookies Of hoHttpC To True
    // Do not save cookies to files, just keep them in memory.
    Set ComCookieDir Of hoHttpC To "memory"
    Set ComSendCookies Of hoHttpC To True

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbCookiesXml
    If (Not(IsComObjectCreated(hoSbCookiesXml))) Begin
        Send CreateComObject of hoSbCookiesXml
    End
    Get ComLoadFile Of hoSbCookiesXml "c:/temp/cookie_cache/google_com.xml" "utf-8" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSbCookiesXml To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetAsString Of hoSbCookiesXml To sTemp1
    Get ComSetCookieXml Of hoHttpC "google.com" sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttpC To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComQuickGetStr Of hoHttpC "https://google.com/" To sHtml

    // Examine the LastHeader to see the contents of the Cookie header field.
    Get ComLastHeader Of hoHttpC To sTemp1
    Showln sTemp1


End_Procedure