频道分类

Delphi 获取百度注册页面验证码图片的源代码

作者:admin 来源: 日期:2019/10/13 14:03:44 人气: 标签:

 
procedure TForm1.btn1Click(Sender: TObject);
 
  procedure doSomething(ms: TMemoryStream);
  var
    Buffer:Word;
    AjpgFile: TJPEGImage;
  begin
    ms.Position := 0;
    if ms.Size = 0 then
      Exit;
    ms.ReadBuffer(Buffer,2); //读取文件的前2个字节,放到Buffer里面
    if Buffer=$4D42 then //如果前两个字节是以4D42[低位到高位]
    begin
      ShowMessage('BMP'); //那么这个是BMP格式的文件
    end
    else if Buffer=$D8FF then //如果前两个字节是以D8FF[低位到高位]
     begin
      ShowMessage('JPEG'); //........一样 下面不注释了
      ms.Position := 0;
      AjpgFile := TJPEGImage.Create;
      AjpgFile.LoadFromStream(ms);
      Image1.Picture.Graphic := AjpgFile;
     end
    else if Buffer=$4947 then
    begin
      ShowMessage('GIF');
    end
    else if Buffer=$5089 then
    begin
      ShowMessage('PNG');
    end;
  end;
var
  Openurl : string;
  elem: IHTMLElement;
  coll: IHTMLElementCollection;
  i: Integer;
  url, Text: string;
  d2,D:IHTMLDocument2;
  d1:IHTMLDocument;
  e:IHTMLElement;
  e2:IHTMLElement2;
  cp: IHTMLControlRange;
  img:IHTMLImgElement;
  ce:IHTMLControlElement;
  bmp:TBitmap ;
  r0:TRect;
  newbmp:TBitmap;
  r1:TRect;
  checkstr:string;
  MyHandle :THandle ;
  bmpPtr:Pointer;
  ms:TMemoryStream;   //内存流对象
begin
  ms := TMemoryStream.Create;  //建立内存流对象
  IdHTTPBaiDu := TIdHTTP.Create(nil);
  IdHTTPBaiDu.ReadTimeout := 2000000;
  IdHTTPBaiDu.IOHandler := IdSSLIOHandlerSocketOpenSSL1.Create(nil);
  Openurl := 'https://passport.baidu.com/v2/?reg&u=http://www.baidu.com/&tpl=mn';
//  Openurl := 'http://zc.qq.com/chs/index.html';
//    Openurl := 'http://www.doubao.com/user/reg';//豆包网
  try
    BaiDuWebBrowser.Navigate(Openurl);
    while BaiDuWebBrowser.Busy do
    begin
      Application.ProcessMessages;
    end;
    BaiDuWebBrowser.Stop;
    if BaiDuWebBrowser.Document = nil then
      Exit;
    //获取源代码
    D := BaiDuWebBrowser.Document as IHTMLDocument2;
    e :=d.body as IHTMLElement;
    e2 :=e as IHTMLElement2;
    cp :=e2.createControlRange as IHTMLControlRange;
    d2 :=BaiDuWebBrowser.Document as IHTMLDocument2;
    //下面是破解验证码
    coll := d.all;
    coll :=(coll.tags('img') as IHTMLElementCollection);
    for i := 0 to coll.Length - 1 do
    begin
    //循环取出每个url
      elem := (coll.item(i,0) as IHTMLElement);
      url :=Trim(string(elem.getAttribute(WideString('src'), 0)));
      //Text := Trim(string(elem.outertext));
      if Pos('doubao' , url) > 0 then  //豆包网
      begin
        if Pos('auth/checkcode',url) > 0 then
        begin
          IdHTTPBaiDu.Get(url,ms);
          doSomething(ms);
          Break;
        end
      end
      else if Pos('qq.com' , url) > 0 then   //腾讯QQ
      begin
        if pos('getimage', url) > 0 then
        begin
          IdHTTPBaiDu.Get(url,ms);
          doSomething(ms);
          Break;
        end
      end
      else if Pos('baidu.com' , url) > 0 then //百度
      begin
        if Pos('genimage',url) > 0then
        begin
          IdHTTPBaiDu.Get(url,ms);
          doSomething(ms);
          Break;
        end;
      end;
    end;
  finally
  
  end;
end;
用到的控件:TWebBrowser、IDhttp、IdSSLIOHandlerSocketOpenSSL还有一个image负责显示验证码图片。
 indy使用ssl时要用openssl动态库,libeay32.dll,ssleay32.dll

来源:https://blog.csdn.net/sushengmiyan/article/details/8064089