微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我们有一个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消息”.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐