procedure TForm1.Button1Click(Sender: TObject);
begin
if WebRequestHandler < nil then
WebRequestHandler.WebModuleClass := WebModuleClass;
Application.Initialize;
Application.Run;
end;
3、使用 EMS (Enterprise Mobility Services)
Delphi 10+ 提供了 EMS 框架用于创建 REST 服务器:
delphi
uses
EMS.Services;
procedure TForm1.Button1Click(Sender: TObject);
begin
EMSRun;
end;
procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
if ARequestInfo.Document = '/' then
begin
AResponseInfo.ContentText := '<html<body<h1Delphi HTTP Server</h1</body</html';
AResponseInfo.ContentType := 'text/html';
end
else if ARequestInfo.Document = '/api' then
begin
AResponseInfo.ContentText := '{status:OK,data:Hello World}';
AResponseInfo.ContentType := 'application/json';
end
else
begin
AResponseInfo.ResponseNo := 404;
AResponseInfo.ContentText := 'Not Found';
end;
end;