gpt4 book ai didi

delphi - 将 TIDCookieManager 中的 Cookie 保存到文件中

转载 作者:行者123 更新时间:2023-12-03 15:23:35 26 4
gpt4 key购买 nike

如何将 TIdCookieManager 中的 cookie 保存到文件中以便以后使用?就像浏览器cookie一样。

最佳答案

TIdCookieManager 没有任何对在文件中保存 cookie 数据的 native 支持。您必须手动实现。使用 TIdCookieManager.CookieCollection 属性访问 cookie 对象列表。例如:

uses
..., IdCookie, IdCookieManager;

var
Cookies: TIdCookieList;
Cookie: TIdCookie;
I: Integer;
begin
Cookies := IdCookieManager.CookieCollection.LockCookieList(caRead);
try
for I := 0 to Cookies.Count-1 do
begin
Cookie := Cookies[I];
// save Cookie properties as needed...
end;
finally
IdCookieManager.CookieCollection.UnlockCookieList(caRead);
end;
end;

.

uses
..., IdCookie, IdCookieManager;

var
Cookies: TIdCookies;
Cookie: TIdCookie;
begin
Cookies := IdCookieManager.CookieCollection.LockCookieList(caReadWrite);
try
for (each saved cookie) do
begin
Cookie := IdCookieManager.CookieCollection.Add;
try
// read Cookie properties as needed...
Cookies.Add(Cookie);
except
Cookie.Free;
raise;
end;
end;
finally
IdCookieManager.CookieCollection.UnlockCookieList(caReadWrite);
end;
end;

或者:

uses
..., IdCookie, IdCookieManager;

var
Cookies: TIdCookieList;
Cookie: TIdCookie;
I: Integer;
S: string;
begin
Cookies := IdCookieManager.CookieCollection.LockCookieList(caRead);
try
for I := 0 to Cookies.Count-1 do
begin
Cookie := Cookies[I];
S := Cookie.ServerCookie;
// save S as needed...
end;
finally
IdCookieManager.CookieCollection.UnlockCookieList(caRead);
end;
end;

.

uses
..., IdCookie, IdCookieManager, IdURI;

var
S: string;
Cookies: TIdCookies;
Cookie: TIdCookie;
Uri: TIdURI;
begin
Cookies := IdCookieManager.CookieCollection.LockCookieList(caReadWrite);
try
for (each saved cookie) do
begin
// read S as needed
S := ...;
Uri := TIdURI.Create(URL where cookie came from);
try
Cookie := IdCookieManager.CookieCollection.Add;
try
Cookie.ParseServerCookie(S, Uri);
Cookies.Add(Cookie);
except
Cookie.Free;
raise;
end;
finally
Uri.Free;
end;
finally
IdCookieManager.CookieCollection.UnlockCookieList(caReadWrite);
end;
end;

关于delphi - 将 TIDCookieManager 中的 Cookie 保存到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14382732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com