将JSON数据从delphi客户端发布到RESTful datasnap服务器

时间:2013-07-27 11:58:44

标签: web-services delphi http rest datasnap

我需要从Delphi客户端向Restful datasnap服务器(Delphi)发送一个简单的JSON对象。我正在使用Delphi XE。任何人都可以帮我解决问题吗?我正在尝试几个小时,但没有得到它..请询问细节是否不充分

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

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;

3 个答案:

答案 0 :(得分:7)

这是一些使用SuperObject通过HTTP Post发送JSON数据的简单XE2测试代码(使用Indy的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('http://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 http://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"}

Receiver是TWebAction上的TWebModule,带有处理程序:

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 (http://stackoverflow.com/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"}

答案 1 :(得分:0)

您可以将Indy的TIdHTTP组件用于Post() JSON编码的数据到REST服务器。您只需要在自己的代码中对JSON数据进行编码和解码。

答案 2 :(得分:0)

从Delphi6发布到XE5内置的数据快照服务器,以上对我有用,但是我发现服务器方法需要将参数定义为TJSONObject而不是TStream。除此之外,Datansap服务器在您执行发布而不是获取操作时会更改查找方法。例如,我有一个名为ImportData的方法。但是,当您调试服务器时,它正在寻找UpdateImportData方法。因此,当我添加新方法并将参数类型更改为Data.DBXJSON.TJSONObject时,它就起作用了。