【问题标题】:Clear Cookies in TChromium在 TChromium 中清除 Cookie
【发布时间】:2016-03-11 23:51:40
【问题描述】:

如何清除 CEF3.1547 中的 cookie 我尝试了以下解决方案,但这根本没有任何作用。 Cookie 仍然存在。还有比这更好的解决方案吗?

procedure TForm1.Button1Click(Sender: TObject);
var
  CookieManager: ICefCookieManager;
begin
  // login to site
  CookieManager := TCefCookieManagerRef.GetGlobalManager;
  CookieManager.VisitAllCookiesProc(
    function(const name, value, domain, path: ustring; secure, httponly,
      hasExpires: Boolean; const creation, lastAccess, expires: TDateTime;
      count, total: Integer; out deleteCookie: Boolean): Boolean
    begin
      deleteCookie := True;
      ShowMessage('A cookie from domain ' + domain + ' will be unmercifully ' +
        'deleted!');
    end
  );
  // visit the site again to see if cookies cleared..
end;

【问题讨论】:

  • 是的。有。 WACEF 中的 cookie 管理器是否支持DeleteCookies 方法?我为 DCEF 1 编写的问题中的代码(that post 的日期早于DCEF 3 project was released)。在 DCEF 1 DeleteCookies 对我不起作用。也许 WACEF 会这样做。
  • 欢迎来到 Stack Overflow。有一个更好的方法来问这个问题。与其问有没有更好的解决方案,不如问如何删除cookies。然后,将您的代码作为答案 发布。您无需询问是否有更好的解决方案。如果有,其他人会将其作为另一个答案发布,人们可以投票决定哪个更好。
  • @TLama delete_cookies 出现在 WACEF。
  • 然后尝试使用它。您正在寻找更好的解决方案。不是我。我写道 DeleteCookies 方法经常让我失败,但它是使用 DCEF 1 的。也许新的 DeleteCookies 方法可以与您正在使用的包装器一起使用。
  • @TLama 我已经测试过DeleteCookies,它在 CEF3 / WACEF 中不起作用。你能发布如何在 IOThread 上运行它吗?

标签: delphi tchromium chromium-embedded


【解决方案1】:

使用此代码从 Chromium 版本 CEF3 中删除 Cookie:

使用 c_WB_ClearCookies 删除所有 Cookies

使用 c_WB_Clear_url_Cookies 来删除所有 Cookie 像这样的网址 -> c_WB_Clear_url_Cookies('http://google.com','cookie_name');

type
  CefTask = class(TCefTaskOwn)
    procedure Execute; override;

    public
    var url,cookieName: ustring;
    constructor create; virtual;
  end;

constructor CefTask.create;
begin
  inherited create;
  url := '';
  cookieName := '';
end;

procedure CefTask.Execute;
var CookieManager: ICefCookieManager;
begin
  CookieManager := TCefCookieManagerRef.Global;
  CookieManager.DeleteCookies(url,cookieName);
end;

procedure c_WB_ClearCookies;
var Task: CefTask;
begin
  Task := CefTask.Create;
  CefPostTask(TID_IO, Task);
end;

// c_WB_Clear_url_Cookies('http://google.com','cookie_name');
procedure c_WB_Clear_url_Cookies(c_url,c_cookieName: ustring);
var Task: CefTask;
begin
  Task := CefTask.Create;
  Task.url := c_url;
  Task.cookieName := c_cookieName;
  CefPostTask(TID_IO, Task);
end;

要列出所有 Cookie 以获取 cookieName,请使用过程 list_all_cookies

procedure pausek;
var M: TMsg;
begin
  while PeekMessage(M, 0, 0, 0, pm_Remove) do
    begin
      TranslateMessage(M);
      DispatchMessage(M);
    end;
end;

procedure pause(i:longint);
var j : nativeint;
begin
  for j := 1 to i do
    begin
      pausek;
      sleep(100);
    end;
end;



procedure list_all_cookies;
var CookieManager: ICefCookieManager;
    cookie_list : string;
const lf = chr(13) + chr(10); 
begin

  cookie_list := '';

  CookieManager := TCefCookieManagerRef.Global;

  CookieManager.VisitAllCookiesProc(

    function(const name, value, domain, path: ustring; secure, httponly,

      hasExpires: Boolean; const creation, lastAccess, expires: TDateTime;

      count, total: Integer; out deleteCookie: Boolean): Boolean

    begin

      cookie_list := cookie_list + inttostr(count) + ': ' +  domain + ' - ' + name + ' - ' + value + ' - ' + path + lf;

     if (count<total) then result := true;

    end

  );

  pause(10);

  ShowMessage(cookie_list);
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2022-05-12
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多