IdHTTPServer(indy10)開發REST中間件
阿新 • • 發佈:2017-09-09
url tid dma bsp method ini mdm tin json格式 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
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
{$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參數
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格式的數據
finally
dm.Free;
end;
end;
except
on E: Exception do
begin
end;
end;
{$ifdef ado}
CoUninitialize;
{$endif}
end;
IdHTTPServer(indy10)開發REST中間件