本文介绍了将TIDCookieManager中的Cookies保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 TIdCookieManager
中的cookie保存到文件中以便以后使用?像浏览器cookie一样。
How can I save cookies inside TIdCookieManager
to a file so that they can be used later? Like browser cookies.
推荐答案
TIdCookieManager
不提供对本机的任何支持。将Cookie数据持久保存在文件中。您必须手动实现。使用 TIdCookieManager.CookieCollection
属性访问cookie对象的列表。例如:
TIdCookieManager
does not have any native support for persisting cookie data in files. You have to implement that manually. Use the TIdCookieManager.CookieCollection
property to access the list of cookie objects. For example:
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;
这篇关于将TIDCookieManager中的Cookies保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!