IE Cookie 文件格式

这几天在网上无意间看到很多人转载或部分截取了本文的部分内容
在此声明,转载请注明出处,谢谢

前几天写了个工具 CookieAdmin 来查看 IE 的 Cookie
有人问到实现原理,就写了这篇文章
我不善于写文档或文章,所以还是代码说话吧

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

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

 4function
 GetCookieFolder: string;
 5var

 6  P: array[0..MAX_PATH] of Char;
 7begin

 8  SHGetFolderPath(0, CSIDL_COOKIES, 00, @P[0]);
 9  Result :=
 IncludeTrailingBackslash(P);
10end;
注意 shell32 常量定义在 ShellAPI.pas 里,CSIDL_COOKIES 定义在 ShlObj.pas 里,记得引用

枚举 Cookie 文件
GetCookieFiles
 1procedure GetCookieFiles(APath: string; AList:TStrings);
 2var

 3  Sr: TSearchRec;
 4begin

 5  if FindFirst(APath + '*.txt', faArchive, Sr) = 0 then
 6  begin
 7    repeat
 8      if Sr.Name[1= '.' then Continue;
 9

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

13
    FindClose(Sr);
14  end
;
15end;

下面才是重点,Cookie 文件的格式,呵呵
Cookie 文件只是个纯粹的文本文件,以换行符(ASCII=10)为分隔符
可以使用 TStringList 读取,会自动分行的
格式如下
a_cookie
0.123
my.demo.site
1600
1589052800
30634450
672816768
29899592
*

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

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

ConvertToDateTime
 1function FileTimeToDateTime(FT: TFileTime): TDateTime; inline;
 2var
 3  ST: TSystemTime;
 4begin
 5  FileTimeToLocalFileTime(FT, FT);
 6  FileTimeToSystemTime(FT, ST);
 7  Result := SystemTimeToDateTime(ST);
 8end;
 9
10function ConvertToDateTime(L, H: Cardinal): TDateTime;
11var
12  FT: TFileTime;
13begin
14  FT.dwLowDateTime := L;
15  FT.dwHighDateTime := H;
16  Result := FileTimeToDateTime(FT);
17end;


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

转载于:https://www.cnblogs.com/sephil/archive/2008/05/06/cookiefmt.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值