IE 的 Cookie 文件保存在 ?:\Documents and Settings\<user>\Cookies 目录,后缀为.txt
可以直接使用 API SHGetFolderPath 取得 Cookie 文件的保存目录
不过我没发现 Delphi2007 有这个 API 的声明,所以自己声明了一下
代码如下(发现代码高亮支持 Pascal 了,呵呵)


GetCookieFolder
实现代码如下:

function SHGetFolderPath(hwndOwner: HWND; nFolder: Integer; hToken: HWND;
dwFlags: Word; pszPath: PChar): Boolean; stdcall; external shell32 name 'SHGetFolderPathA';

function GetCookieFolder: string;
var
P: array[0..MAX_PATH] of Char;
begin
SHGetFolderPath(0, CSIDL_COOKIES, 0, 0, @P[0]);
Result := IncludeTrailingBackslash(P);
end;

注意 shell32 常量定义在 ShellAPI.pas 里,CSIDL_COOKIES 定义在 ShlObj.pas 里,记得引用

枚举 Cookie 文件
GetCookieFiles
实现代码如下:

procedure GetCookieFiles(APath: string; AList:TStrings);
var
Sr: TSearchRec;
begin
if FindFirst(APath + '*.txt', faArchive, Sr) = 0 then
begin
repeat
if Sr.Name[1] = '.' then Continue;

AList.Add(Sr.Name);
until FindNext(Sr) <> 0;

FindClose(Sr);
end;
end;

下面才是重点,Cookie 文件的格式,呵呵
Cookie 文件只是个纯粹的文本文件,以换行符(ASCII=10)为分隔符
可以使用 TStringList 读取,会自动分行的
格式如下
实现代码如下:

a_cookie
.123
my.demo.site

*

其中
第1行为 Cookie 名称
第2行是 Cookie 的值
第3行是 Cookie 所属站点的地址
第4行是个标记值(注:准确来说应该是表示该Cookie是否被加密)
第5行为超时时间的低位(Cardinal/DWORD)
第6行为超时时间的高位
第7行为创建时间的低位
第8行为创建时间的高位
第9行固定为 * ,表示一节的结束
需要注意的是这里使用的时间并非 Delphi 的 TDateTime,而是 FILETIME(D里为对应的TFileTime)
一个文件可能包含有多个节,按上面的格式循环即可

下面的代码将上述时间转换为 D 里的 TDateTime


ConvertToDateTime
实现代码如下:

function FileTimeToDateTime(FT: TFileTime): TDateTime; inline;
var
ST: TSystemTime;
begin
FileTimeToLocalFileTime(FT, FT);
FileTimeToSystemTime(FT, ST);
Result := SystemTimeToDateTime(ST);
end;

function ConvertToDateTime(L, H: Cardinal): TDateTime;
var
FT: TFileTime;
begin
FT.dwLowDateTime := L;
FT.dwHighDateTime := H;
Result := FileTimeToDateTime(FT);
end;


怎么样,确实很简单吧?呵呵

以上就是【IE Cookie文件格式说明】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)

与本文相关的软件

发表我的评论

最新评论

  1. 暂无评论