我用idhttp的get方法获取网站上的一个验证码图片,获取成功了,但该网站同时会返回一个cookie对应这个验证码图片。用工具抓取封包时看到有:Set-Cookie: verifysession=4d9c35a7431c412eb84dad847e1873a310ad9ef0fa884321fabd757124e377c159919de23adea034; 而问题是idhttp的CookieManager.CookieCollection.Count的值竟然为0,也就是说没有获得cookie,但网站的确是返回cookie了啊,这是怎么回事啊?没有获得这个cookie我无法进行下一步操作。郁闷中获取图片的源码如下: function bweb.GetJpg(url:string;referer:string):TJpegImage;
var
ms:TMemoryStream;
bmp:TJpegImage;
// s:string;
begin
try
ms:=TMemoryStream.Create;
self.http.Request.Referer:=referer;
http.AllowCookies:=true;
self.http.Get(url,ms);
ms.Position:=0;
bmp:=TJpegImage.Create;
bmp.LoadFromStream(ms);
ms.Destroy;
// s:=http.Response.RawHeaders.Text ;//在这里可以看到cookie值,晕,但是idhttp并没有获得
result:=bmp;
except
result:=nil;
end;
end;
var
ms:TMemoryStream;
bmp:TJpegImage;
// s:string;
begin
try
ms:=TMemoryStream.Create;
self.http.Request.Referer:=referer;
http.AllowCookies:=true;
self.http.Get(url,ms);
ms.Position:=0;
bmp:=TJpegImage.Create;
bmp.LoadFromStream(ms);
ms.Destroy;
// s:=http.Response.RawHeaders.Text ;//在这里可以看到cookie值,晕,但是idhttp并没有获得
result:=bmp;
except
result:=nil;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
ImageFile:TFileStream;
begin
ImageFile:=TFileStream.Create(ExtractFilePath(ParamStr(0))+'Temp.bmp',fmCreate);
try
FHTTP.Get('http://ui.ptlogin2.qq.com/cgi-bin/login?link_target=blank&target=self&appid=8000203&f_url=loginerroralert&s_url=http%3A//imgcache.qq.com/club/portal_new/redirect.html%3Fjump_url%3D');
FHTTP.Get('http://ptlogin2.qq.com/getimage',ImageFile);
ShowMessage(IntToStr(IdCookieManager1.CookieCollection.Count));
ShowMessage(IdCookieManager1.CookieCollection.Items[0].ClientCookie);
finally
if Assigned(ImageFile) then FreeAndNil(ImageFile);
end;
ShellExecute(Self.Handle,'open',PChar(ExtractFilePath(ParamStr(0))+'Temp.bmp'),nil,nil,SW_SHOWNORMAL) ;
end;帮你弄好了,哈
其实get一次就会返回cookie了,我是想知道为什么返回了还不能使用
http是我定义的tidhttp变量,下面不会有错啊
这个HTTP.CookieManager.CookieCollection.Items[0].CookieText应该就能获得一个cookie值了啊
procedure TForm1.Button2Click(Sender: TObject);
var
ImageFile:TFileStream;
begin
ImageFile:=TFileStream.Create(ExtractFilePath(ParamStr(0))+'Temp.bmp',fmCreate);
try
FHTTP.Get('http://ptlogin2.qq.com/getimage',ImageFile);
ShowMessage(IntToStr(IdCookieManager1.CookieCollection.Count));
ShowMessage(IdCookieManager1.CookieCollection.Items[0].ClientCookie);
finally
if Assigned(ImageFile) then FreeAndNil(ImageFile);
end;
ShellExecute(Self.Handle,'open',PChar(ExtractFilePath(ParamStr(0))+'Temp.bmp'),nil,nil,SW_SHOWNORMAL) ;
end;一次Get我也试了下可以,Indy版本是9.0.18
HTTP/1.1 200 OK
Server: tencent http server
Accept-Ranges: bytes
Pragma: No-cache
Content-Length: 2730
Set-Cookie: verifysession=b72618c1edafd357f231d7afa82f7615a766e1f126a519d652f1e737eedc07d95dc0f3b755b801a6; PATH=/; DOMAIN=qq.com;
Connection: close
Content-Type: image/jpeg
procedure TForm1.Button4Click(Sender: TObject);
var
ms:TMemoryStream;
bmp:TJpegImage;
FHTTP:TIdHTTP;
begin
try
FHTTP:=TIdHTTP.Create(nil);
ms:=TMemoryStream.Create;
try
FHTTP.AllowCookies:=true;
FHTTP.Get('http://ptlogin2.qq.com/getimage',ms);
//ms.Position:=0;
//bmp:=TJpegImage.Create;
//bmp.LoadFromStream(ms);
ShowMessage(FHTTP.Response.RawHeaders.Text); //这里面有Coookie,
finally
FreeAndNil(FHTTP);
FreeAndNil(MS);
end;
except
end;
end;
难道内存里面的临时cookie就处理不了了吗?非得是硬盘上的cookie吗?这样的话idhttp太逊色了吧!
感觉Delphi版块真的太冷清了,没什么活力了!问问题都没几个人回答。唉
如果在C#板块问问题,就有很多人站出来解答了,而这里就很冷清了,可以看得出C#比delphi热门很多。期待delphi雄起!!!!!!!!!!!!!!!!!!
my blog is www.hacksoft.cn