我用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;

解决方案 »

  1.   

    该图片的网址为:http://ptlogin2.qq.com/getimage?aid=15000701&0.9129723031485226大家帮忙研究测试一下啊
      

  2.   

    不少人都在做QQ的模拟登陆嘛,QQ军刀不错,呵呵
      

  3.   


    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;帮你弄好了,哈
      

  4.   

    为什么要get两次啊?
    其实get一次就会返回cookie了,我是想知道为什么返回了还不能使用
      

  5.   

    返回了不能使用是你下面代码的问题,另开帖,用2次Get是QQ站点设计的问题,第一次就有Cookie,取验证的时候要带上第一次的Cookie,
      

  6.   

    不会吧?
    http是我定义的tidhttp变量,下面不会有错啊
    这个HTTP.CookieManager.CookieCollection.Items[0].CookieText应该就能获得一个cookie值了啊
      

  7.   

    我是用工具抓取封包的时候看到有返回的,但好像idhttp并没有处理好吧
      

  8.   


    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
      

  9.   

    这是get网络图片时抓取的封包代码
    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
      

  10.   

    为什么要用你的函数,我不是贴出函数了,上面的我已经测试了,Indy9.0.18,Delphi7,Get一次,可以,
      

  11.   


    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;
      

  12.   

    我的函数是写在一个没有窗体的类里面,该类定义了一个Tidhttp变量,专门用于访问网页用的,而该函数是返回一张验证码图片。获得图片同时cookie自动保存在Tidhttp变量里
      

  13.   

    其实我用c#做过,很容易搞定,但在delphi下就不知道问题出在什么地方了,名名有cookie返回,却没有保存下来,不知道什么回事??
    难道内存里面的临时cookie就处理不了了吗?非得是硬盘上的cookie吗?这样的话idhttp太逊色了吧!
      

  14.   

    没有满意的答案啊!
    感觉Delphi版块真的太冷清了,没什么活力了!问问题都没几个人回答。唉
    如果在C#板块问问题,就有很多人站出来解答了,而这里就很冷清了,可以看得出C#比delphi热门很多。期待delphi雄起!!!!!!!!!!!!!!!!!!
      

  15.   

    没有满意的答案,但还是得结贴,感谢各位的帮忙,特别是hidelphi ,谢谢了!
      

  16.   

    indy10就搞定了楼主  
    my blog is www.hacksoft.cn