Delphi Datasnap Post请求

File- New -other


在Form上面添加控件:

IdHTTPServer 并实现方法IdHTTPServer1CommandGet

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo:
    TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  ADataJson:TJSONObject;
  aRequestContent:string;
begin
  if SameText(ARequestInfo.Document,'/api/JmzService/ExuBill') = True then
  begin

    if UpperCase(ARequestInfo.Command) = 'POST' THEN
    begin
  //    strPlist:= TStrings.Create();
      arequestinfo.CharSet := 'UTF-8';

      if  (arequestinfo.PostStream <> nil ) and (arequestinfo.PostStream.Size >0 ) then
      begin
        arequestinfo.PostStream.Position := 0;
        aRequestContent := StreamToString(ARequestInfo.PostStream);
      end;

      aRequestContent := TIdURI.URLDecode(aRequestContent);     //中文解码

  //    strPlist := ARequestInfo.Params;

      ADataJson:= TJSONObject.Create().AddPair('code','0').AddPair('msg','susscss').AddPair('info','测试').AddPair('token','84848ED0-A72D-4304-ADA0-001F2B7B3173');
  //    strPlist.Free;
      AResponseInfo.ContentType := 'text/html; charset=GB2312';
      AResponseInfo.CharSet := 'UTF-8';  //指定中文字符集
      AResponseInfo.ContentText:=  ADataJson.ToString;
    end;
  end
  else
  begin
   //404
      AResponseInfo.CharSet := 'UTF-8';  //指定中文字符集
      AResponseInfo.ResponseNo := 404;

      AResponseInfo.ContentText := '找不到' + ARequestInfo.Document;

  end;

在启动加上

procedure TForm1.StartServer;
begin
  if not FServer.Active then
  begin
    FServer.Bindings.Clear;
    FServer.DefaultPort := StrToInt(EditPort.Text);
    FServer.Active := True;
    IdHTTPServer1.Active := True;//非常重要
  end;
end;

posted @ 2021-08-16 15:33  三弄梅花  阅读(314)  评论(0编辑  收藏  举报