加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 可以通过URI传递RemObjects SDK参数吗?

发布时间:2020-12-15 09:33:45 所属栏目:大数据 来源:网络整理
导读:我们有一个RemObjects SDK HTTP服务器,它公开了许多服务和方法.是否可以通过URI调用方法,而不是将参数作为SOAP / JSON传递,例如 http://www.mywebservice.com/servicename/methodname?param1=xxxparam2=yyy 解决方法 这是 norgepaul’s解决方案的一个看起来
我们有一个RemObjects SDK HTTP服务器,它公开了许多服务和方法.是否可以通过URI调用方法,而不是将参数作为SOAP / JSON传递,例如

http://www.mywebservice.com/servicename/methodname?param1=xxx&param2=yyy

解决方法

这是 norgepaul’s解决方案的一个看起来很好并返回JSON的游戏.它基于使用TROIndyHTTPServer的后代拦截HTTP请求的相同想法,但这次我不仅修复请求的参数,我正在创建客户端未发送的“JSON”帖子!

这是我用默认的“VCL Standalon”服务器实现测试的代码:

TUriROIndyHTTPServer = class(TROIndyHTTPServer)
protected
  procedure InternalServerCommandGet(AThread: TIdThreadClass; RequestInfo: TIdHTTPRequestInfo; ResponseInfo: TIdHTTPResponseInfo); override;
end;

procedure TUriROIndyHTTPServer.InternalServerCommandGet(AThread: TIdThreadClass;RequestInfo: TIdHTTPRequestInfo; ResponseInfo: TIdHTTPResponseInfo);
var A,B: Integer;
    NewPost: AnsiString;
begin
  if RequestInfo.Document = '/json/sum' then
    begin
      // Extract the parameters
      A := StrToIntDef(RequestInfo.Params.Values['a'],0);
      B := StrToIntDef(RequestInfo.Params.Values['b'],0);
      NewPost := AnsiString(Format('{"version":"1.1","method":"NewService.Sum","params":{"A":"%d","B":"%d"}}',[A,B]));

      // Prepare the (fake) post-stream
      RequestInfo.PostStream.Free;
      RequestInfo.PostStream := TMemoryStream.Create;
      RequestInfo.PostStream.Write(NewPost[1],Length(NewPost));
      RequestInfo.PostStream.Position := 0;
    end
  else if RequestInfo.Document = '/json/getservertime' then
    begin
      // Extract the parameters
      NewPost := '{"version":"1.1","method":"NewService.GetServerTime"}';

      // Prepare the (fake) post-stream
      RequestInfo.PostStream.Free;
      RequestInfo.PostStream := TMemoryStream.Create;
      RequestInfo.PostStream.Write(NewPost[1],Length(NewPost));
      RequestInfo.PostStream.Position := 0;
    end;

  inherited;
end;

有了这种代码,我可以提出这样的请求:

http://localhost:8080/json/sum?a=1&b=2

返回(在浏览器中!)

{"version":"1.1","result":"3"}

还有这个:

http://localhost:8080/json/getservertime

返回这个(好吧,在撰写本文时):

{"version":"1.1","result":"2013-02-01T19:24:24.827"}

>我的服务器主表单pastebin link的整个代码
>我的服务器主表单pastebin link的DFM

结果(在浏览器或外部应用程序中)是纯JSON,因为它使用RO的代码形成为“JSON消息”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读