• 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;
    
    
  • 相关阅读:
    18.10.29 考试总结
    【BZOJ】2730: [HNOI2012]矿场搭建
    BZOJ [ZJOI2007]仓库建设
    18.10.24 考试总结
    ZOJ 3740 Water Level
    洛谷 P2474 [SCOI2008]天平
    洛谷 P4180 【模板】严格次小生成树[BJWC2010]
    CF961E Tufurama
    18.10.22 考试总结
    18.10.19 考试总结
  • 原文地址:https://www.cnblogs.com/Ken2018/p/15148127.html
Copyright © 2020-2023  润新知