我们有一个 RemObjects SDK HTTP 服务器,它公开了许多服务和方法。是否可以通过 URI 调用方法,而不是将参数作为 SOAP/JSON 传递,例如
http://www.mywebservice.com/servicename/methodname?param1=xxx¶m2=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"}
- 我的服务器主表单的完整代码 pastebin link
- 我的服务器主表单的 DFM pastebin link
结果(在浏览器或外部应用程序中)是纯 JSON,因为它已使用 RO 的代码格式化为“JSON 消息”。
关于delphi - RemObjects SDK参数可以通过URI传递吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14645022/