打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
DataSnap与JSON序列化(4)

delphi prism作为datasnap client可以调用delphi写的REST Server中的方法。

首先,可能是测试版的缘故,某些datasnap的组件,如TAdoDbxDatasnapConnection,在工具箱中没有安装,需要手动安装,C:/Program Files/Common Files/CodeGear Shared/Delphi Prism/Shared Assemblies/7.0/Borland.Data.AdoDBXClient.dll

datasnap调用写法:

  1. method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);  
  2. var  
  3.   C:TAdoDbxDatasnapConnection;  
  4.   vC:TADODBXCommand;  
  5. begin  
  6.   C:=new TAdoDbxDatasnapConnection();  
  7.   try  
  8.       C.ConnectionString:=  
  9.         'CommunicationProtocol=http;'+  
  10.         'HostName='+textBox_Server.Text+';'+  
  11.         'Port='+textBox_Port.Text;  
  12.       C.Open;  
  13.         
  14.       vC:=C.CreateCommand as TADODBXCommand;  
  15.       try   
  16.           vC.CommandType:=System.Data.CommandType.StoredProcedure;// TDBXCommandTypes.DSServerMethod;  
  17.           vC.CommandText:='TServerMethods1.ReverseString';  
  18.           vC.Prepare;  
  19.           vC.Parameters[0].Value:=textBox_RawString.Text;  
  20.           vC.ExecuteNonQuery;  
  21.           textBox_Result.Text:=vC.Parameters[1].Value.ToString;  
  22.       finally  
  23.           vC.Dispose;  
  24.       end;  
  25.        
  26.   finally  
  27.       C.Dispose;  
  28.   end;  
  29. end;  

http get的写法是:

[c-sharp] view plaincopy?
  1. method MainForm.button3_Click(sender: System.Object; e: System.EventArgs);  
  2. var  
  3.     J:TJSONObject;  
  4.     S:string;  
  5.     vURL:string;  
  6.     vHC:RemObjects.InternetPack.Http.HttpClient;  
  7. begin  
  8.     vURL:='http://'+textBox_Server.Text+':'+textBox_Port.Text+'/datasnap/rest/TServerMethods1/ReverseString/';  
  9.     vURL:=vURL+system.Uri.EscapeDataString(textBox_RawString.Text);  
  10.     vHC:=new RemObjects.InternetPack.Http.HttpClient();  
  11.     S:=vHC.Get(vURL);  
  12.     //textBox_Result.Text:=S;  
  13.     J:=new Borland.Data.TJSONObject();  
  14.     J.Parse(System.Text.Encoding.ASCII.GetBytes(S),0);  
  15.     textBox_Result.Text:=TJSONArray(J.Get(0).JsonValue).Get(0).Value;  
  16. end;  

注意参数的编码,需要使用函数system.Uri.EscapeDataString()来编码。

比如,参数:AB CDE上海FG、//?/,会被转义为:AB%20CDE%E4%B8%8A%E6%B5%B7FG%E3%80%81%5C%2F%3F%5C

 

出现一个问题,

当用localhost这样调用时,没有问题,得到:{"result":[//?/////u3001GF/u6D77/u4E0AEDC BA]}

http://localhost:8080/datasnap/rest/TServerMethods1/ReverseString/AB%20CDE%E4%B8%8A%E6%B5%B7FG%E3%80%81%5C%2F%3F%5C

 

但是如果用外网的IP来调用时,就出现错误:

http://210.x.x.x:8080/datasnap/rest/TServerMethods1/ReverseString/AB%20CDE%E4%B8%8A%E6%B5%B7FG%E3%80%81%5C%2F%3F%5C

错误信息是: 500 Internal Server Error,The request was rejected by the HTTP filter. Contact the server administrator. (12217)

 

而参数换成简单的ABCD就可以:

http://210.x.x.x:8080/datasnap/rest/TServerMethods1/ReverseString/ABCD

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
delphi xe2做的datasnap 三层,如何实现客户端的心跳。
Delphi2010中DataSnap高级技术(1)—为DataSnap系统服务程序添加描...
奇迹重现 Delphi 2005赏析-4
C#问题向数据库添加一个datatime类型的数据
dataGridView中的复制
C#批量操作控件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服