首页 » 文章随笔 » 技巧教程 » delphi中如何删除dcef3的cookie和缓存?

delphi中如何删除dcef3的cookie和缓存?

我们用以下代码打开网站

var
   Chromium: TChromium;
begin
   try
     Chromium := TChromium.Create(nil);
     Chromium.SetParentComponent(Form1);
     Chromium.Align := alClient;
     chromium.Browser.MainFrame.LoadUrl('www.bnwin.com');
     FreeAndNil(Chromium)
end;

如何删除dcef3的cookie和缓存?请看以下代码

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;

整理自网站 https://stackoverflow.com/questions/12269587/how-do-i-clear-the-cache-and-cookies-for-an-embedded-chromium-browser

取得cookie并显示,如果需要删除cookie,把deleteCookie:= False改为deleteCookie:= True

function VisitCookie (
const name, value, domain, path: ustring; secure, httponly, hasExpires: Boolean;
const creation, lastAccess, expires: TDateTime; count, total: Integer;
out deleteCookie: Boolean): Boolean;
begin
  deleteCookie:= False;
  MainForm.Memo1.Lines. Add (' cookie ' +inttostr (count) + ' / ' + inttostr (total));
  MainForm.Memo1.Lines. Add (' name ' +name);
  MainForm.Memo1.Lines. Add (' value ' +value);
  MainForm.Memo1.Lines. Add (' domain ' +domain);
  MainForm.Memo1.Lines. Add (' path ' +path);
  MainForm.Memo1.Lines. Add (' secure ' +BoolToStr (secure));
  MainForm.Memo1.Lines. Add (' httponly ' +BoolToStr (httponly));
  MainForm.Memo1.Lines. Add (' hasExpires ' +BoolToStr (hasExpires));
  MainForm.Memo1.Lines. Add (' creation ' +DateToStr (creation));
  MainForm.Memo1.Lines. Add (' lastAccess ' +DateToStr (lastAccess));
  MainForm.Memo1.Lines. Add (' expires ' +DateToStr (expires));
  MainForm.Memo1.Lines. Add ('--------------- ');
  Result:= True;
end;

procedure TMainForm.btn2Click(Sender: TObject);
var CookieManager: ICefCookieManager;
begin
CookieManager:= TCefCookieManagerRef.Global(nil);
CookieManager.VisitAllCookiesProc (VisitCookie);
end;

//这样写,我在delphi xe8中编译不过
procedure TMainForm.Button1Click(Sender: TObject);
var
  CookieManager: ICefCookieManager;
begin
  CookieManager := TCefCookieManagerRef.Global(nil);
  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
  );
end;

为网址设置单独的cookie文件

CookieManager: ICefCookieManager;

FormCreate:
begin
   CookiesPath := ExtractFilePath(Application.ExeName) + 'cookies/bnwin';
   CookieManager := TCefCookieManagerRef.Global(nil);
   CookieManager.SetStoragePath(CookiesPath, True, nil);
   Chromium1.Load('bnwin.com'); 
end;

FormClose:   
begin
  CookieManager.FlushStore(nil);
end

为指定的网站设置cookie

var Fsuccess: Boolean;
procedure SetCookieR(success: Boolean);
begin
   Fsuccess := success;
end;

procedure TMainForm.btn1Click(Sender: TObject);
var CookieManager: ICefCookieManager;
begin
  CookieManager := TCefCookieManagerRef.Global(nil);
  CookieManager.SetCookieProc('http://bnwin.com', 'TEST', 'supertest', '.bnwin.com', '/', False,False, True, now, now, StrToDateTime('2020-12-30'),SetCookieR);
end;

dcef中允许网站保存cookie会话

CookieManager: ICefCookieManager;

FormCreate:
begin
   CookiesPath := ExtractFilePath(Application.ExeName) + 'cookies';
   CookieManager := TCefCookieManagerRef.Global(nil);
   CookieManager.SetStoragePath(CookiesPath, True, nil);
end;

FormClose:   
begin
  CookieManager.FlushStore(nil);
end

 

未经允许不得转载:百脑问 » delphi中如何删除dcef3的cookie和缓存?

×

感谢您的支持,我们会一直保持!

扫码支持
请土豪扫码随意打赏

打开微信扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

分享到:更多 ()

评论 抢沙发

评论前必须登录!

百脑问 创新坚持 杰然不同

赞助我们联系我们
数据查询次数:18 消耗时间:0.563 秒, 使用内存: 20.99MB
您好,有什么可以帮您吗!