PureBasic
PureBasic
Example: Http.SetCookieXml method
Demonstrates theSetCookieXml method.
Chilkat PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
Procedure ChilkatExample()
success.i = 0
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkSaveCookies(http, 1)
CkHttp::setCkCookieDir(http, "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.
CkHttp::setCkSendCookies(http, 0)
; Do a request that establishes and saves cookies in files in the cookie directory.
html.s = CkHttp::ckQuickGetStr(http,"https://google.com/")
; Examine the LastResponseHeader to see the cookies that were received:
Debug CkHttp::ckLastResponseHeader(http)
Debug "----------------------------------"
; 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.
Debug CkHttp::ckGetCookieXml(http,"google.com")
Debug "----------------------------------"
; 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:
httpB.i = CkHttp::ckCreate()
If httpB.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkSaveCookies(httpB, 1)
CkHttp::setCkCookieDir(httpB, "c:/temp/cookie_cache")
CkHttp::setCkSendCookies(httpB, 1)
; The cookies from the cache are sent.
html = CkHttp::ckQuickGetStr(httpB,"https://google.com/")
; Examine the LastHeader to see the contents of the Cookie header field.
Debug CkHttp::ckLastHeader(httpB)
Debug "----------------------------------"
; -------------------------------------------------------------------------------------------
; Another way to do it is to explicitly load the cookies from XML
; and set the cookies for the particular domain.
httpC.i = CkHttp::ckCreate()
If httpC.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkSaveCookies(httpC, 1)
; Do not save cookies to files, just keep them in memory.
CkHttp::setCkCookieDir(httpC, "memory")
CkHttp::setCkSendCookies(httpC, 1)
sbCookiesXml.i = CkStringBuilder::ckCreate()
If sbCookiesXml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkStringBuilder::ckLoadFile(sbCookiesXml,"c:/temp/cookie_cache/google_com.xml","utf-8")
If success = 0
Debug CkStringBuilder::ckLastErrorText(sbCookiesXml)
CkHttp::ckDispose(http)
CkHttp::ckDispose(httpB)
CkHttp::ckDispose(httpC)
CkStringBuilder::ckDispose(sbCookiesXml)
ProcedureReturn
EndIf
success = CkHttp::ckSetCookieXml(httpC,"google.com",CkStringBuilder::ckGetAsString(sbCookiesXml))
If success = 0
Debug CkHttp::ckLastErrorText(httpC)
CkHttp::ckDispose(http)
CkHttp::ckDispose(httpB)
CkHttp::ckDispose(httpC)
CkStringBuilder::ckDispose(sbCookiesXml)
ProcedureReturn
EndIf
html = CkHttp::ckQuickGetStr(httpC,"https://google.com/")
; Examine the LastHeader to see the contents of the Cookie header field.
Debug CkHttp::ckLastHeader(httpC)
CkHttp::ckDispose(http)
CkHttp::ckDispose(httpB)
CkHttp::ckDispose(httpC)
CkStringBuilder::ckDispose(sbCookiesXml)
ProcedureReturn
EndProcedure