[Gambas-user] gb.qt5.webkit Cookies

Benoît Minisini g4mba5 at gmail.com
Sun Oct 7 20:09:28 CEST 2018


Le 23/08/2018 à 06:55, james at lixce.com a écrit :
> Has anyone been able to store and read cookies with qt5.webkit?
> 
> I'm iterating through WebView1.Cookies and writting each field and a
> key to a file when I close the form.
> 
> Public Sub CookieJar_Store() Dim cFile As File Dim macaroon As
> Cookie
> 
> File.Save("/home/james/macaroons.txt", "") cFile = Open
> "/home/james/macaroons.txt" For Write Output To cFile For Each
> macaroon In WebView1.Cookies Print "Domain: " & macaroon.Domain Print
> "Expire: " & macaroon.ExpirationDate Print "HttpON: " &
> macaroon.HttpOnly Print "Name: " & macaroon.Name Print "Path: " &
> macaroon.Path Print "Secure: " & macaroon.Secure Print "Session: " &
> macaroon.Session Print "Value: " & macaroon.Value Next
> 
> Output To Default Close cFile End
> 
> Then when on open I read them back into an array, then copy that
> array to WebView1.Cookies (kind ov verbose,trying to figure out waht
> is happening Public Sub CookieJar_Fill()
> 
> Dim cFile As File Dim sLine As String Dim macaroon As New Cookie Dim
> myCookies As New Cookie[]
> 
> If Not Exist("/home/james/macaroons.txt") Then Return
> 
> cFile = Open "/home/james/macaroons.txt" For Input
> 
> While Not Eof(cFile)
> 
> Line Input #cFile, sLine 'sLine = Replace(sLine, "\n", "") If sLine
> Begins "Domain: " Then macaroon.Domain = String.Right(sLine, -8) 
> Endif If sLine Begins "Expire: " Then macaroon.ExpirationDate =
> String.Right(sLine, -8) Endif If sLine Begins "HttpON: " Then 
> macaroon.HttpOnly = String.Right(sLine, -8) Endif If sLine Begins
> "Name: " Then macaroon.Name = String.Right(sLine, -6) Endif If sLine
> Begins "Path: " Then macaroon.Path = String.Right(sLine, -6) Endif If
> sLine Begins "Secure: " Then macaroon.Secure = String.Right(sLine,
> -8) Endif If sLine Begins "Value: " Then macaroon.Value
> =String.Right(sLine, -7) myCookies.Add(macaroon) Endif Wend
> 
> Close cFile
> 
> WebView1.Cookies = myCookies
> 
> Print "Cookie Count: " & WebView1.Cookies.Count End
> 
> When I Print the cookies to the console using a sub similar to the
> CookieJar_Store, after loading, they look the same as the text file
> (minus the keys of course).  But Google, Amazon, NFCU refuse to
> accept them, the sites act the same as if there are no cookies at
> all.   I don't think I need to store the session cookies but makes no
> difference if I don't.
> 
> I have looked at the WebBrowser example and Gentleman Borwser but
> niether of them do anything with cookies.  There is an old project
> called Aredia that is written in Gambas but I cannot figure out what
> they were doing with cookies.
> 
> So, anyone figured out a way to save and restore cookies in
> gb.qt5.webkit.
> 
> Thanks, -j
> 

Your code can't work: you create only one cookie object when loading
your file, and your final cookie array contains several times the same
cookie object.

You must create one cookie object for each cookie stored in your text file.

Maybe it won't change nothing, I don't know. But who can guess what
QtWebkit does with an array of several times the same cookie object?

Regards,


-- 
Benoît Minisini


More information about the User mailing list