benibela / internettools

XPath/XQuery 3.1 interpreter for Pascal with compatibility modes for XPath 2.0/XQuery 1.0/3.0, custom and JSONiq extensions, pattern matching, XML/HTML/JSON parsers and classes for HTTP/S requests
http://www.benibela.de/sources_en.html#internettools
122 stars 35 forks source link

Cookies #7

Closed kopr12 closed 7 years ago

kopr12 commented 8 years ago

Are cookies implemented in internetaccess ? I can't seem to find it and I really miss that feature from synapse.

http://synapse.ararat.cz/doc/help/httpsend.THTTPSend.html#Cookies

benibela commented 8 years ago

They are handled automatically, too:

https://github.com/benibela/internettools/blob/master/internet/internetaccess.pas#L129-L137

Or you can set a cookie header

kopr12 commented 8 years ago

Awesome, thanks !

kopr12 commented 8 years ago

Okay, no problem to set cookie header. But how can I view them ?

benibela commented 8 years ago

With the public functions, you cannot

You would need to read the protected array. It is not public, because I might change the cookie implementation

kopr12 commented 8 years ago

Something like cookies.SaveToFile / cookies.LoadFromFile would be great.

kopr12 commented 8 years ago

I did it manually with defaultinternet.lastHTTPHeaders.SaveToFile , to view them, and then set them later with defaultInternet.additionalHeaders.Text , and that works fine. Not a big problem but it would be nice to have that feature, simple to implement. I'm not sure if I should leave this issue open or close it, up to you I guess.

Great package overall btw, I'm really impressed. Thanks for providing it !

benibela commented 7 years ago

I have added a "TCookieManager" with SaveToFile/LoadFromFile methods.

Viewing individual cookies is not possible as there might be many cookies with the same name.

kopr12 commented 7 years ago

Nice, however I can't figure out how to work with that, maybe you can point me in the right direction. Let's take this for example :

var
 res : string; 
begin
 res := httpRequest('https://youtube.com/');
 defaultinternet.lastHTTPHeaders.SaveToFile('cookies.log');
end;

If you look at cookies.log you will see 3 cookies.

Can you please tell me how to save cookies with TCookieManager ? I tried several things but I always end up with empty file.

benibela commented 7 years ago

You do

 defaultinternet.cookies.saveToFile('/tmp/cookies.log');          
kopr12 commented 7 years ago

Working like a charm 👍

Don't know how I missed it, for some reason I was doing : cm: TCookieManager; cm.saveToFile... cm.parseHeadersForCookies... etc.

Thanks !