Reputation: 219
I am using a TNetHTTPClient in a Delphi 10.2.3 Firemonkey project and would like to clear all stored cookies. I did not find any solution in the help files. I tried this code, but I get the error that the array is read-only:
SetLength(NetHTTPClient1.CookieManager.Cookies, 0);
What can I do to clear all cookies, without destroying the instance of TNetHTTPClient and creating it again?
Upvotes: 0
Views: 940
Reputation: 367
Just an idea:
for i := 0 to High(NetHTTPClient1.CookieManager.Cookies)
do NetHTTPClient1.CookieManager.Cookies[i].Expires := Now - 1;
NetHTTPClient1.CookieManager.dCookies;
This way you can set all cookies as expired.
Getting cookies again call GetCookies
that internally call DeleteExpiredCookies
Edit
Unfortunetely this will not work (read comments below for details)
Upvotes: 1
Reputation: 219
I figured it out, many thanks to BigBother and Toon Krijthe in another thread about class helpers!
As CookieManager.Cookies
is read-only, I tried to gain access to the private field TCookies
via a class helper, which does not work anymore since 10.1 Berlin. However, Toon Krijthe found a way, and I adapted it to suit my needs:
Interface:
type
TCookieManagerHelper = class helper for TCookieManager
procedure DeleteCookies;
end;
Implementation:
procedure TCookieManagerHelper.DeleteCookies;
begin
with self do
FCookies.clear;
end;
Whenever I want to clear cookies:
NetHTTPClient1.CookieManager.DeleteCookies;
I have to add that this might not work with future versions of Delphi, as Embarcadero disabled accessing private fields via class helpers on purpose.
Upvotes: 0