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

标签 delphi uri delphi-xe2 remobjects

我们有一个 RemObjects SDK HTTP 服务器,它公开了许多服务和方法。是否可以通过 URI 调用方法,而不是将参数作为 SOAP/JSON 传递,例如

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

最佳答案

这是 norgepaul's 上的一个游戏看起来不错并返回 JSON 的解决方案。它基于使用 TROIIndyHTTPServer 的后代拦截 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"}

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

关于delphi - RemObjects SDK参数可以通过URI传递吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14645022/

相关文章:

delphi - 更改名称作为输入给出的变量 (Pascal)

multithreading - (Delphi 2009) idIRC、MDI 和挂起问题

delphi - 这种设计模式的名称是什么?

delphi - 如何让 Delphi 不打扰我的 DFM?

delphi - maxint 在哪里定义 - 重复定义?

delphi - 从 Delphi 2009 升级的原因

delphi - Delphi 中的 ADO 错误 'Operation cancelled'

http - jdk.incubator.httpclient URI 查询字符串在包含编码的保留字符时被截断

xml - Visual Studio > XML 解析器 > 注册一个 URI 前缀

android - 白色上下文菜单(复制/粘贴)