function GetCookieFolder: string;
const
SCookieKey = '/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders';
var
Reg: TRegistry;
begin
Reg := TRegistry.Create; Result := '';
try Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey(SCookieKey, False) then Result := Reg.ReadString('Cookies');
finally Reg.Free;
end;
end;
function FindCookie(HostName, FileName : string):string;
var
Tsl : TStringList;
i : integer;
begin
Result:='';
Tsl := TStringList.Create;
try Tsl.LoadFromFile(FileName);
except
end;
for i:=2 to Tsl.Count-1 do begin
if pos(HostName,Tsl)=1 then begin
if Result<>'' then Result:=Result+';';
Result:= Result+Tsl[i-2]+'='+Tsl[i-1];
end;
end;
Tsl.Free;
end;
function GetCookie( HostName: string ):string;
var
Found: Integer;
SearchRec: TSearchRec;
CookieFolder: string;
begin
HostName:=LowerCase(HostName);
CookieFolder := GetCookieFolder;
if (Copy(CookieFolder,Length(CookieFolder),1)<>'/') then CookieFolder:=CookieFolder+'/';
Found := FindFirst(CookieFolder + '*.txt', faAnyFile, SearchRec);
while Found = 0 do begin
if (SearchRec.Attr and faDirectory) = 0 then begin
Result:=FindCookie(HostName, CookieFolder + SearchRec.Name);
if Result<>'' then break;
end;
Found := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
win2000下使用正常,其他系统未测试过。
使用: cookies:=GetCookie('163.com');