delphi 通过idhttp实现登录验证  
官方Delphi 学习QQ群: 682628230(三千人)\n
频道

delphi 通过idhttp实现登录验证


最近在做客户端的时候,遇到一个问题,登录验证的时候,因为我用的是mysql数据库,需要安装驱动,而这个驱动每个用户都得去装才行,这样就不太适合用户体验了,最后我决定通过idhttp来实现这一功能,具体如下:
首先,要在pas中引用:IdTCPConnection, IdTCPClient, IdHTTP,IdCookieManager, IdAntiFreezeBase, IdAntiFreeze
 
然后在按钮的click中构造参数,通过idhttp的post方法传递给服务器(注:post过去之后,通过传过去的用户名跟密码去数据库查询,然后在客户端接收信息,通过判断返回值,给出提示信息,由于布尔值不太安全,这里可以通过字符串来验证)。
 
在此,贴上部分主要代码:
 
procedure TStart1.Button1Click(Sender: TObject);
Var
 Response:string;
 Paramstr:Tstringlist;
 IdHTTP1:Tidhttp;
begin
 IdHTTP1:=Tidhttp.create(nil);
 Paramstr:= TStringList.Create;
 Paramstr.Add('username='+Username.Text);
 Paramstr.Add('password='+Userpass.Text);
 IdHTTP1.Request.Referer := 'http://www.cctvkf.com/uu.php';
 IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
 IdHTTP1.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; QQDownload 714)';
 IdHttp1.Request.SetHeaders;
 IdHTTP1.HTTPOptions:=[hoKeepOrigProtocol];
 Response:=IdHTTP1.Post('http://www.cctvkf.com/uu.php', Paramstr);
 if Response='222' then
 begin
    Application.MessageBox('用户名或者密码错误','温馨提示',0);
    exit;
 end;
  FRecv := TRecvData.Create(NIL, Server.Text, StrToInt(Port.Text));
  FRecv.OnTerminate := DisRecv;
  FRecv.OnDataCuLi := CuLi;
  FRecv.Resume;
 Freeandnil(IdHTTP1);
 Paramstr.Free;
end;

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.046622037887573 seconds