Search code examples
httpdelphicookiesfiremonkey

TNetHTTPClient clear cookies


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?


Solution

  • 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.