Sample code for 30+ languages & platforms
PowerBuilder

Example: Http.SetCookieXml method

Demonstrates the SetCookieXml method.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
string ls_Html
oleobject loo_HttpB
oleobject loo_HttpC
oleobject loo_SbCookiesXml

li_Success = 0

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Http.SaveCookies = 1
loo_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.
loo_Http.SendCookies = 0

// Do a request that establishes and saves cookies in files in the cookie directory.
ls_Html = loo_Http.QuickGetStr("https://google.com/")

// Examine the LastResponseHeader to see the cookies that were received:
Write-Debug loo_Http.LastResponseHeader
Write-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.
Write-Debug loo_Http.GetCookieXml("google.com")
Write-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:
loo_HttpB = create oleobject
li_rc = loo_HttpB.ConnectToNewObject("Chilkat.Http")

loo_HttpB.SaveCookies = 1
loo_HttpB.CookieDir = "c:/temp/cookie_cache"
loo_HttpB.SendCookies = 1

// The cookies from the cache are sent.
ls_Html = loo_HttpB.QuickGetStr("https://google.com/")

// Examine the LastHeader to see the contents of the Cookie header field.
Write-Debug loo_HttpB.LastHeader
Write-Debug "----------------------------------"

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

loo_HttpC = create oleobject
li_rc = loo_HttpC.ConnectToNewObject("Chilkat.Http")

loo_HttpC.SaveCookies = 1
// Do not save cookies to files, just keep them in memory.
loo_HttpC.CookieDir = "memory"
loo_HttpC.SendCookies = 1

loo_SbCookiesXml = create oleobject
li_rc = loo_SbCookiesXml.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_SbCookiesXml.LoadFile("c:/temp/cookie_cache/google_com.xml","utf-8")
if li_Success = 0 then
    Write-Debug loo_SbCookiesXml.LastErrorText
    destroy loo_Http
    destroy loo_HttpB
    destroy loo_HttpC
    destroy loo_SbCookiesXml
    return
end if

li_Success = loo_HttpC.SetCookieXml("google.com",loo_SbCookiesXml.GetAsString())
if li_Success = 0 then
    Write-Debug loo_HttpC.LastErrorText
    destroy loo_Http
    destroy loo_HttpB
    destroy loo_HttpC
    destroy loo_SbCookiesXml
    return
end if

ls_Html = loo_HttpC.QuickGetStr("https://google.com/")

// Examine the LastHeader to see the contents of the Cookie header field.
Write-Debug loo_HttpC.LastHeader


destroy loo_Http
destroy loo_HttpB
destroy loo_HttpC
destroy loo_SbCookiesXml