我们有一个RemObjects SDK HTTP服务器,它公开了许多服务和方法.是否可以通过URI调用方法,而不是将参数作为SOAP /
JSON传递,例如
http://www.mywebservice.com/servicename/methodname?param1=xxx¶m2=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消息”.