用API模拟登录网站后想清除Cookie怎么办呢?

dcrr64 2009-02-12 12:10:02
我调用InternetOpen,InternetConnect,HttpOpenRequest,HttpSendRequest这几个API做了个软件验证登录帐号是否正确,每次登录网站前我想清除掉Cookie再登录不用上次登录的Cookie要怎么做呢?

function GetWebPage(const URLA,URLB,FTPostQuery:string):string;
var
Session,
hConnect,hRequest:HINTERNET;
szSizeBuffer:Pointer;
dwLengthSizeBuffer:DWord;
dwReserved:DWord;
dwFileSize:DWord;
dwBytesRead:DWord;
Contents:PChar;
AcceptType:LPStr;
TOPA:String;
begin
Session:=InternetOpen('',0,niL,niL,0);
hConnect := InternetConnect(Session,Pchar(URLA),80, nil, nil,INTERNET_SERVICE_HTTP, 0, 0);
AcceptType := PChar('Accept: */*');
hRequest := HttpOpenRequest(hConnect, 'POST',PChar(URLB), 'HTTP/1.0',nil, @AcceptType, INTERNET_FLAG_RELOAD, 0);
//----------------------------------------------------------
TOPA:='Content-Type: application/x-www-form-urlencoded';
HttpAddRequestHeaders(hRequest,PChar(TOPA),Length(TOPA),HTTP_ADDREQ_FLAG_ADD);
TOPA:='Accept-Language: zh-cn';
HttpAddRequestHeaders(hRequest,PChar(TOPA),Length(TOPA),HTTP_ADDREQ_FLAG_ADD);
TOPA:='Referer: http://www.163.com';
HttpAddRequestHeaders(hRequest,PChar(TOPA),Length(TOPA),HTTP_ADDREQ_FLAG_ADD);
//----------------------------------------------------------
HttpSendRequest(hRequest, nil,0,PChar(FTPostQuery), Length(FTPostQuery));
dwLengthSizeBuffer:=1024;
HttpQueryInfo(hRequest,5,szSizeBuffer,dwLengthSizeBuffer,dwReserved);
GetMem(Contents,dwFileSize);
InternetReadFile(hRequest,Contents,dwFileSize,dwBytesRead);
InternetCloseHandle(hRequest);
InternetCloseHandle(Session);
InternetCloseHandle(hConnect);
Result:=StrPas(Contents);
FreeMem(Contents);
end;
...全文
653 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanqth 2009-02-12
  • 打赏
  • 举报
回复
cookie的文件夹路径保存在注册表
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Cookies 这一项里面
tanqth 2009-02-12
  • 打赏
  • 举报
回复
WINDOWS的COOKIE都是一样的,其实还是用的IE的.
function GetCookiesFolder:string;
var
pidl:pItemIDList;
buffer:array [ 0..255 ] of char ;
begin
SHGetSpecialFolderLocation(
application.Handle , CSIDL_COOKIES, pidl);
SHGetPathFromIDList(pidl, buffer);
result:=strpas(buffer);
end;
function ShellDeleteFile(sFileName: string): Boolean;
var
FOS: TSHFileOpStruct;
begin
FillChar(FOS, SizeOf(FOS), 0); {记录清零}
with FOS do
begin
wFunc := FO_DELETE;//删除
pFrom := PChar(sFileName);
fFlags := FOF_NOCONFIRMATION;
end;
Result := (SHFileOperation(FOS) = 0);
end;

//删除cookies
procedure DelCookie;
var
dir:string;
begin
try
InternetSetOption(nil, INTERNET_OPTION_END_BROWSER_SESSION, nil, 0);
dir:=GetCookiesFolder;
ShellDeleteFile(dir+'\*.txt'+#0);
except
abort;
end;

end; function GetCookiesFolder:string;
var
dcrr64 2009-02-12
  • 打赏
  • 举报
回复
这样删除的好像是IE的COOKIE吧?这个API用的不是IE的Cookie不知道他是放哪里的
bdmh 2009-02-12
  • 打赏
  • 举报
回复
可以试一下将cookies文件夹内容全部删除,如将C:\Documents and Settings\Administrator\Cookies文件夹下文件及文件夹全部删除,这里的Administrator是你登陆windows的用户名,这个应该很好获取

//取得目录
function GetCookiesFolder:string;
var
pidl:pItemIDList;
buffer:array [ 0..255 ] of char ;
begin
SHGetSpecialFolderLocation(
application.Handle , CSIDL_COOKIES, pidl);
SHGetPathFromIDList(pidl, buffer);
result:=strpas(buffer);
end;
function ShellDeleteFile(sFileName: string): Boolean;
var
FOS: TSHFileOpStruct;
begin
FillChar(FOS, SizeOf(FOS), 0); {记录清零}
with FOS do
begin
wFunc := FO_DELETE;//删除
pFrom := PChar(sFileName);
fFlags := FOF_NOCONFIRMATION;
end;
Result := (SHFileOperation(FOS) = 0);
end;

//删除cookies
procedure DelCookie;
var
dir:string;
begin
try
InternetSetOption(nil, INTERNET_OPTION_END_BROWSER_SESSION, nil, 0);
dir:=GetCookiesFolder;
ShellDeleteFile(dir+'\*.txt'+#0);
except
abort;
end;

end;
开拓者-lada 2009-02-12
  • 打赏
  • 举报
回复

procedure DeleteIECache; // 清理IE缓存,IE.cookies
var
lpEntryInfo: PInternetCacheEntryInfo;
hCacheDir: LongWord;
dwEntrySize: LongWord;
cachefile: string;
i: integer;
cancheqqlist: TStringList;
begin
cancheqqlist := TStringList.Create;
cancheqqlist.Clear;
dwEntrySize := 0;
FindFirstUrlCacheEntry(nil, TInternetCacheEntryInfo(nil^), dwEntrySize);
GetMem(lpEntryInfo, dwEntrySize);
if dwEntrySize > 0 then
lpEntryInfo^.dwStructSize := dwEntrySize;
hCacheDir := FindFirstUrlCacheEntry(nil, lpEntryInfo^, dwEntrySize);
if hCacheDir <> 0 then
begin
repeat
if (lpEntryInfo^.CacheEntryType) and (NORMAL_CACHE_ENTRY) = NORMAL_CACHE_ENTRY then
cachefile := pchar(lpEntryInfo^.lpszSourceUrlName);
if (pos('www.juntnet.net', cachefile) > 0) or (pos('gamelint.xml', cachefile) > 0) then //清除特定网站的cookies.例如.www.kumusic.net.cn
cancheqqlist.Add(cachefile);
for i := 0 to cancheqqlist.Count - 1 do
DeleteUrlCacheEntry(pchar(cancheqqlist.Strings[i])); //执行删除操作
FreeMem(lpEntryInfo, dwEntrySize);
dwEntrySize := 0;
FindNextUrlCacheEntry(hCacheDir, TInternetCacheEntryInfo(nil^), dwEntrySize);
GetMem(lpEntryInfo, dwEntrySize);
if dwEntrySize > 0 then
lpEntryInfo^.dwStructSize := dwEntrySize;
until not FindNextUrlCacheEntry(hCacheDir, lpEntryInfo^, dwEntrySize);
end;
FreeMem(lpEntryInfo, dwEntrySize);
FindCloseUrlCache(hCacheDir);
cancheqqlist.Free;
end;
开拓者-lada 2009-02-12
  • 打赏
  • 举报
回复
procedure DeleteIECache; // 清理IE缓存,IE.cookies
var
lpEntryInfo: PInternetCacheEntryInfo;
hCacheDir: LongWord;
dwEntrySize: LongWord;
cachefile: string;
i: integer;
cancheqqlist: TStringList;
begin
cancheqqlist := TStringList.Create;
cancheqqlist.Clear;
dwEntrySize := 0;
FindFirstUrlCacheEntry(nil, TInternetCacheEntryInfo(nil^), dwEntrySize);
GetMem(lpEntryInfo, dwEntrySize);
if dwEntrySize > 0 then
lpEntryInfo^.dwStructSize := dwEntrySize;
hCacheDir := FindFirstUrlCacheEntry(nil, lpEntryInfo^, dwEntrySize);
if hCacheDir <> 0 then
begin
repeat
if (lpEntryInfo^.CacheEntryType) and (NORMAL_CACHE_ENTRY) = NORMAL_CACHE_ENTRY then
cachefile := pchar(lpEntryInfo^.lpszSourceUrlName);
if (pos('www.juntnet.net', cachefile) > 0) or (pos('gamelint.xml', cachefile) > 0) then //清除特定网站的cookies.例如.www.kumusic.net.cn
cancheqqlist.Add(cachefile);
for i := 0 to cancheqqlist.Count - 1 do
DeleteUrlCacheEntry(pchar(cancheqqlist.Strings[i])); //执行删除操作
FreeMem(lpEntryInfo, dwEntrySize);
dwEntrySize := 0;
FindNextUrlCacheEntry(hCacheDir, TInternetCacheEntryInfo(nil^), dwEntrySize);
GetMem(lpEntryInfo, dwEntrySize);
if dwEntrySize > 0 then
lpEntryInfo^.dwStructSize := dwEntrySize;
until not FindNextUrlCacheEntry(hCacheDir, lpEntryInfo^, dwEntrySize);
end;
FreeMem(lpEntryInfo, dwEntrySize);
FindCloseUrlCache(hCacheDir);
cancheqqlist.Free;
end;

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧