I can’t seem to be able to get cookies working on an roVideoPlayer object (implements ifHttpAgent). I have mocked up some really simple code to try to narrow it down, but even my stripped down code doesn’t work as expected. Does anyone have a working example of AddCookie(), or some pointers to share?
Here’s what I have tried:
player = CreateObject("roVideoPlayer")
expires = CreateObject("roDateTime")
expires.FromSeconds( expires.asSeconds() + 3600 )
' Using an empty string domain (If domain is an empty string, all domains are matched)
player.AddCookies([{version:1, name:"thecookie", value:"the value", domain: "", path:"/", expires: expires}])
' This should print an roArray containing one roAssociativeArray element - instead it prints an empty roArray
Print "player.getCookies(): " player.getCookies(".google.com", "/")
Thanks - I tried again after inserting player.EnableCookies() before adding cookies … it did affect the boolean value returned by AddCookie() (true if cookies are enabled, false otherwise), but GetCookies() is still returning empty.
I don’t think the behavior is caused by a flawed call to GetCookies(): I tested the same logic against a server while monitoring http traffic and the cookie header isn’t showing there either
Here’s my general flow for adding previously stored cookies to a new roUrlTransfer object:
' Getting some useful cookies
Function login(hostname As String)
http = CreateObject("roUrlTransfer")
http.SetUrl("http://" + hostname + "/...")
http.EnableCookies()
http.AsyncPostFromString(...)
'... wait for response
http.GetCookies(hostname, "/")
End Function
' Using those cookies later
Function usecookies(hostname As String)
http = CreateObject("roUrlTransfer")
http.SetUrl("http://" + hostname + "/...")
http.EnableCookies()
http.AddCookies(cookies)
http.AsyncGetToString()
...
End Function
This works for me after a little trial and error. I believe the key is to make sure the hostname lines up everywhere. If you are setting a cookie for an empty domain, it may ignore it or at least not return it when you ask for cookies from a different domain. Cookie domain matching is a security-sensitive subject. Another hint is that we know it’s libcurl under the covers. Looking at curl’s documentation on cookie management might give more insight.
OK thanks for all the help. I got it to work with 2 changes overall
• EnableCookie() doc says “Causes any Set-Cookie headers returned from the request to be interpreted and the resulting cookies to be added to the cookie cache” - but it turns out to be necessary also when adding cookies manually via AddCookie()
• Empty domain names make sense for GetCookies(), but don’t make sense for AddCookies()
Here is a working version of the code
expires = CreateObject("roDateTime")
expires.FromSeconds( expires.asSeconds() + 3600 )
cookie = {version:1, name:"thecookies", value:"thevalue", domain: domain, path:path, expires: expires}
player = CreateObject("roVideoPlayer")
player.EnableCookies()
player.AddCookies([cookie])
player = http.GetCookies("", "/")
if cookies.count() = 1
Print "YEP"
else
Print "NOPE"
end if