从Delphi客户端将JSON数据发布到RESTful Datasnap服务器

编程入门 行业动态 更新时间:2024-10-11 09:29:03
本文介绍了从Delphi客户端将JSON数据发布到RESTful Datasnap服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要将一个简单的JSON对象从Delphi客户端发送到Restful数据快照服务器(Delphi)。我正在使用Delphi XE。有人可以帮我解决代码问题吗?我正在尝试几个小时,但没有得到..请询问详细信息是否不够

I need to send a simple JSON object to a Restful datasnap server (Delphi) from a Delphi client. I am using Delphi XE. Can anybody help me out with the code? I am trying for hours but not getting it.. Please ask if details are not sufficient

编辑:这是服务器端方法声明:

Here is server side method declaration:

procedure updatemethodnme(str:string):string;

这是客户端代码:

function PostData(request: string): boolean; var param: TStringList; url, Text,str: string; code: Integer; http: TIDHttp; begin Result:= false; http:= TIDHttp.Create(nil); http.HandleRedirects:= true; http.ReadTimeout:= 50000; http.request.Connection:= 'keep-alive'; str:= '{"lamp":"'+lamp+'","floor":"'+floor+'","op":"'+request+'"}'; param:= TStringList.Create; param.Clear; param.Add(str); url:= 'h***p://xx2.168.xx.xx:xxxx/Datasnap/rest/TserverMethods1/methdname/'; try Text:= http.Post(url, param); Result:= true; except on E: Exception do begin Result := false; end; end; end;

推荐答案

下面是一些简单的XE2测试代码,可通过HTTP发送JSON数据使用SuperObject发布(使用Indy的 TIdHTTP ):

Here's some simple XE2 test code sending JSON data through HTTP Post using SuperObject (using Indy's TIdHTTP):

procedure TFrmTTWebserviceTester.Button1Click(Sender: TObject); var lJSO : ISuperObject; lRequest: TStringStream; lResponse: String; begin // Next 2 lines for Fiddler HTTP intercept: IdHTTP.ProxyParams.ProxyServer := '127.0.0.1'; IdHTTP.ProxyParams.ProxyPort := 8888; lJSO := SO('{"name": "Henri Gourvest", "vip": true, "telephones": ["000000000", "111111111111"], "age": 33, "size": 1.83, "adresses": [ { "adress": "blabla", "city": "Metz", "pc": 57000 }, { "adress": "blabla", "city": "Nantes", "pc": 44000 } ]}'); lRequest := TStringStream.Create(lJSO.AsString, TEncoding.UTF8); try IdHTTP.Request.ContentType := 'application/json'; IdHTTP.Request.Charset := 'utf-8'; try lResponse := IdHTTP.Post('127.0.0.1:8085/ttposttest', lRequest); ShowMessage(lResponse); except on E: Exception do ShowMessage('Error on request:'#13#10 + E.Message); end; finally lRequest.Free; end; lJSO := nil; end;

这是输出的数据:

POST 127.0.0.1:8085/ttposttest HTTP/1.0 Content-Type: application/json; charset=utf-8 Content-Length: 204 Connection: keep-alive Host: 127.0.0.1:8085 Accept: text/html, */* Accept-Encoding: identity User-Agent: Mozilla/3.0 (compatible; Indy Library) {"vip":true,"age":33,"telephones":["000000000","111111111111"],"adresses":[{"adress":"blabla","pc":57000,"city":"Metz"},{"adress":"blabla","pc":44000,"city":"Nantes"}],"size":1.83,"name":"Henri Gourvest"}

接收器是 TWebModule 上的 TWebAction ,具有处理程序:

Receiver is a TWebAction on a TWebModule, with handler:

procedure TWebModuleWebServices.WebModuleWebServicesTTPostTestAction( Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); var S : String; lJSO: ISuperObject; begin S := Request.Content; if S <> '' then lJSO := SO('{"result": "OK", "encodingtestcharacters": "Typed € with Alt-0128 Will be escaped to \u20ac"}') else lJSO := SO('{"result": "Error", "message": "No data received"}'); Response.ContentType := 'application/json'; // Designating the encoding is somewhat redundant for JSON (stackoverflow/questions/9254891/what-does-content-type-application-json-charset-utf-8-really-mean) Response.Charset := 'utf-8'; Response.Content := lJSO.AsJSON; Handled := true; end; { WebModuleWebServicesTTPostTestAction }

它使用 TIdHTTPWebBrokerBridge :

FWebBrokerBridge := TIdHTTPWebBrokerBridge.Create(Self); // Register web module class. FWebBrokerBridge.RegisterWebModuleClass(TWebModuleWebServices); // Settings: FWebBrokerBridge.DefaultPort := 8085;

这是实际的响应:

HTTP/1.1 200 OK Connection: close Content-Type: application/json; charset=utf-8 Content-Length: 92 {"encodingtestcharacters":"Typed\u20acwithAlt0128FollowedBy8364escaped\u8364","result":"OK"}

更多推荐

从Delphi客户端将JSON数据发布到RESTful Datasnap服务器

本文发布于:2023-11-25 09:14:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   服务器   数据   Datasnap   Delphi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!