unit HttpServer; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdHTTPServer, IdBaseComponent, IdComponent, IdCustomTCPServer, IdCustomHTTPServer, IdContext, Vcl.StdCtrls; type TForm2 = class(TForm) IdHTTPServer1: TIdHTTPServer; Memo1: TMemo; procedure IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation {$R *.dfm} procedure TForm2.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); begin Memo1.Lines.Add( 'Http Command : '+ ARequestInfo.Command ); try AResponseInfo.ContentType := 'application/json'; AResponseInfo.ContentText := 'Hello from server' + sLineBreak; AResponseInfo.ContentText := AResponseInfo.ContentText + '{' + sLineBreak + ' "ip": "192.192.241.1",' + sLineBreak + ' "hostname": "No Hostname",' + sLineBreak + ' "city": null, ' + sLineBreak + ' "country": "TW",' + sLineBreak + ' "loc": "23.5000,121.0000",' + sLineBreak + ' "org": "AS1659 Taiwan Academic Network (TANet) Information Center"' + sLineBreak + '}'; Memo1.Lines.Add('從Client端送來的參數資料:' + ARequestInfo.Params.Text); finally end; end; end.
AResponseInfo.ContentType 要傳給Http客戶端的資料型態 AResponseInfo.ContentText 要傳給Http客戶端的資料內容
ARequestInfo.Params 取得Http客戶端傳來的參數資料 ARequestInfo.Params.Text 參數整個資料字串 資料是以TStringList封裝, Param1=Data1 Param2=Data2 Param3=Data3 ...
要分別取上面的資料可用 ARequestInfo.Params.Values['Param1'],傳回Data1 ARequestInfo.Params.Values['Param2'],傳回Data2 …