分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

IdHTTPServer(indy10)开发REST中间件

发布时间:2023-09-06 01:09责任编辑:赖小花关键词:暂无标签

IdHTTPServer(indy10)开发REST中间件

浏览器通过“get”方式查询数据URL样例:http://127.0.0.1:7777/query?sql=select * from t1

/query 表示是“查询”命令字

?后面是SQL参数

服务端代码:

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
?method, sql: string;
{$IFDEF ?ado}
?dm: TfrmDMado;
{$ENDIF}
{$IFDEF odac}
?dm: TfrmDModac;
{$ENDIF}
begin
?method := ARequestInfo.Document;  // 获取命令字
?if method = ‘‘ then
???Exit;
?{$ifdef ado}
?CoInitialize(nil);
?{$endif}
?try
???if method = ‘/query‘ then  // 查询数据
???begin
?????sql := ARequestInfo.Params.Values[‘sql‘];  // 取SQL参数
?????if sql = ‘‘ then
???????Exit;
?????AResponseInfo.ResponseNo := 0;
?????AResponseInfo.ContentType := ‘text/html‘;
{$IFDEF ado}
?????dm := TfrmDMado.Create(nil);
{$ENDIF}
{$IFDEF odac}
?????dm := TfrmDModac.Create(nil);
{$ENDIF}
?????try
???????AResponseInfo.ContentText := dm.Query(sql);  // 返回数据引擎查询到的JSON格式的数据
???????AResponseInfo.WriteContent;  // 给申请者回复
?????finally
???????dm.Free;
?????end;
???end;
?except
???on E: Exception do
???begin

???end;
?end;
?{$ifdef ado}
?CoUninitialize;
?{$endif}
end;

IdHTTPServer(indy10)开发REST中间件

原文地址:http://www.cnblogs.com/hnxxcxg/p/7497146.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved