最近在做客户端的时候,遇到一个问题,登录验证的时候,因为我用的是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;