Delphi Datasnap Post请求  
官方Delphi 学习QQ群: 682628230(三千人)
频道

Delphi Datasnap Post请求


File- New -other

1168091-20210816153029452-1436056599.png


在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;



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.1603901386261 seconds