IE Cookie文件格式說(shuō)明

字號(hào):


    IE 的 Cookie 文件保存在 ?:\Documents and Settings\<user>\Cookies 目錄,后綴為.txt
    可以直接使用 API SHGetFolderPath 取得 Cookie 文件的保存目錄
    不過(guò)我沒(méi)發(fā)現(xiàn) Delphi2007 有這個(gè) API 的聲明,所以自己聲明了一下
    代碼如下(發(fā)現(xiàn)代碼高亮支持 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;
    下面才是重點(diǎn),Cookie 文件的格式,呵呵
    Cookie 文件只是個(gè)純粹的文本文件,以換行符(ASCII=10)為分隔符
    可以使用 TStringList 讀取,會(huì)自動(dòng)分行的
    格式如下
    a_cookie
    .123
    my.demo.site
    *
    其中
    第1行為 Cookie 名稱
    第2行是 Cookie 的值
    第3行是 Cookie 所屬站點(diǎn)的地址
    第4行是個(gè)標(biāo)記值(注:準(zhǔn)確來(lái)說(shuō)應(yīng)該是表示該Cookie是否被加密)
    第5行為超時(shí)時(shí)間的低位(Cardinal/DWORD)
    第6行為超時(shí)時(shí)間的高位
    第7行為創(chuàng)建時(shí)間的低位
    第8行為創(chuàng)建時(shí)間的高位
    第9行固定為 * ,表示一節(jié)的結(jié)束
    需要注意的是這里使用的時(shí)間并非 Delphi 的 TDateTime,而是 FILETIME(D里為對(duì)應(yīng)的TFileTime)
    一個(gè)文件可能包含有多個(gè)節(jié),按上面的格式循環(huán)即可
    下面的代碼將上述時(shí)間轉(zhuǎn)換為 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;
    怎么樣,確實(shí)很簡(jiǎn)單吧?呵呵