delphi7的新生,参与分布式应用开发,调用RESTful API,Json的应用
前言:
1、公司delphi7开发的传统软件还活得好好的,但是大家都知道delphi早已经日落西山了,现在成了后进、追随者。细细算了已经6、7不用了。新的delphixe7呢,没有时间成本去适应和研究。
由于大量使用了第3方的组件和控件,想升级估计是吃力不讨好的事情...
2、保留原有代码,开发新功能可调用远程主机(云主机)的REST ful风格的api,使用Json交换数据。这样就赶上了新潮流,复活了。
由于网上搜索了很多次,发现符合需求的文章很少,这里记录下来,授人以鱼吧。
- delphi7使用Json
- delphi7使用msxml 的“IXMLHttpRequest”对象实现发起 Http post get请求,并传递Json。
- delphi7使用base64编码图片通过Json传递
以下是关键代码:
(1)msxml,SuperObject(第3方,delphi7不自带), EncdDecd 的引用
(2)XMLHttpRequest的使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | //提交会员开卡信息 procedure TFormMakeCard . btnOKClick(Sender: TObject); var url: string ; resultJson,paramsJson: ISuperObject; begin if (CheckInput) then begin HttpReq := CoXMLHTTPRequest . Create; //两种提交请求的方式:Get和Post,Get是通过URL地址传递参数如果是中文需要转码,需要添加时间戳 Post不需要添加时间戳 //get url := 'http://localhost:5269/api/webmemberapi/NewMemberRegister?timestamp='+inttostr(Windows.GetTickCount); url := 'http://localhost:5269/api/webmemberapi/NewMemberRegister?' ; HttpReq . open( 'Post' , url, False , EmptyParam, EmptyParam); //http post HttpReq . setRequestHeader( 'Accept' , 'application/json' ); HttpReq . setRequestHeader( 'Content-Type' , 'application/json' ); //请求主体 try paramsJson:= GetMemberEntity(); Memo1 . Lines . Clear; memo1 . Text := paramsJson . AsString(); HttpReq . send(paramsJson . AsString()); resultJson:=SO(HttpReq . responseText); if (resultJson<> nil ) then begin showmessage(resultJson . S[ 'Message' ]); //发卡成功,如果有照片或者签名则执行上传 if (resultJson . B[ 'Success' ]= true ) then begin if (mbPhoto) then begin url := 'http://localhost:5269/api/webmemberapi/UploadMemberImage?' ; HttpReq . open( 'Post' , url, False , EmptyParam, EmptyParam); //http post HttpReq . setRequestHeader( 'Accept' , 'application/json' ); HttpReq . setRequestHeader( 'Content-Type' , 'application/json' ); paramsJson:=SO(); paramsJson . S[ 'ImageFileContent' ]:= BitmapToString(ImageMemberPhoto . Picture . Bitmap); paramsJson . S[ 'ImageCategory' ]:= '头像' ; paramsJson . S[ 'MemberCardNo' ]:=self . edtCardNo . Text; HttpReq . send(paramsJson . AsString()); resultJson:=SO(HttpReq . responseText); end ; if (mbSign) then begin url := 'http://localhost:5269/api/webmemberapi/UploadMemberImage?' ; HttpReq . open( 'Post' , url, False , EmptyParam, EmptyParam); //http post HttpReq . setRequestHeader( 'Accept' , 'application/json' ); HttpReq . setRequestHeader( 'Content-Type' , 'application/json' ); paramsJson:=SO(); paramsJson . S[ 'ImageFileContent' ]:= BitmapToString(ImageMemberSign . Picture . Bitmap);; paramsJson . S[ 'ImageCategory' ]:= '签名' ; paramsJson . S[ 'MemberCardNo' ]:=self . edtCardNo . Text; HttpReq . send(paramsJson . AsString()); resultJson:=SO(HttpReq . responseText); end ; end ; end ; except on Ex:Exception do showmessage(Ex . Message); end ; end ; end ; |
//XMLHttpRequest实例化
HttpReq := CoXMLHTTPRequest.Create;
//两种提交请求的方式:Get和Post,Get是通过URL地址传递参数如果是中文需要转码,需要添加时间戳 Post不需要添加时间戳
//get url := 'http://localhost:5269/api/webmemberapi/NewMemberRegister?timestamp='+inttostr(Windows.GetTickCount);
url := 'http://localhost:5269/api/webmemberapi/NewMemberRegister?';
// Post或者Get
HttpReq.open('Post', url, False, EmptyParam, EmptyParam);
//http post **这是关键代码**RequestHeader的设置'application/json',服务器端才能识别为Json
HttpReq.setRequestHeader('Accept', 'application/json');
HttpReq.setRequestHeader('Content-Type', 'application/json');
//这是请求的主体
HttpReq.send(paramsJson.AsString());
//把服务器返回的Json字符串反序列化成一个SuperObject对象。
resultJson:=SO(HttpReq.responseText);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Com.Aidpoint.MemberApi.Models { /// <summary> /// Action返回值实体 /// </summary> public class ApiActionResult { public bool Success { get ; set ; } public string Message { get ; set ; } public object Result { get ; set ; } } } |
if (resultJson<>nil) then
begin
showmessage(resultJson.S['Message']);
//发卡成功,如果有照片或者签名则执行上传
if (resultJson.B['Success']=true) then ...
end
(3)Josn包含base64编码的图片:
//位图文件转Base64字符串 function TFormMakeCard.BitmapToString(img:TBitmap):string; var ms:TMemoryStream; ss:TStringStream; s:string; begin ms := TMemoryStream.Create; img.SaveToStream(ms); ss := TStringStream.Create(''); ms.Position:=0; EncodeStream(ms,ss);//将内存流编码为base64字符流 s:=ss.DataString; ms.Free; ss.Free; result:=s; end; url := 'http://localhost:5269/api/webmemberapi/UploadMemberImage?'; HttpReq.open('Post', url, False, EmptyParam, EmptyParam); //http post HttpReq.setRequestHeader('Accept', 'application/json'); HttpReq.setRequestHeader('Content-Type', 'application/json'); paramsJson:=SO(); paramsJson.S['ImageFileContent']:= BitmapToString(ImageMemberPhoto.Picture.Bitmap); paramsJson.S['ImageCategory']:='头像'; paramsJson.S['MemberCardNo']:=self.edtCardNo.Text; HttpReq.send(paramsJson.AsString()); resultJson:=SO(HttpReq.responseText);
(4)服务器端的web api
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | public ApiActionResult NewMemberRegister([FromBody]MemberDTO memberDTO) { //初始化返回值 var result = new ApiActionResult() { Success = false , Result = null , Message = "操作失败。" }; if (memberDTO != null ) { try { using (MemberEntities db = new MemberEntities()) { var dbEntity = CheckMemberExists(memberDTO, db); if (dbEntity== null ) { //插入会员表 var entity = DTO2Entity.ConvertToEntityObject<会员表>(memberDTO) as 会员表; db.AddTo会员表(entity); //生成储值流水--w:误收,d:积分兑换礼品,f:发新卡,c:储值,q:取款,j:积分消费记录 var detailRec = new 会员储值流水表(); detailRec.会员卡号 = memberDTO.卡号; detailRec.储值标志 = "f" ; detailRec.充值金额 = memberDTO.开卡额.Value; detailRec.新卡 = "Y" ; db.AddTo会员储值流水表(detailRec); try { db.SaveChanges(); result.Success = true ; result.Result = entity; result.Message = string .Format( "操作成功。新发会员卡[{0}],开卡金额:{1}。" , entity.自编号, entity.开卡额); } catch (Exception ex) { var exx = ex.InnerException == null ? ex : ex.InnerException; throw new Exception(exx.Message); } } else { result.Success = false ; result.Result = null ; result.Message = string .Format( "卡号[{0}]已经存在。发卡门店[{1}],持卡人:{2}。" ,dbEntity.自编号,dbEntity.发行分店编号,dbEntity.姓名); } } } catch (Exception ex) { var exx = ex.InnerException == null ? ex : ex.InnerException; result.Success = false ; result.Result = exx; result.Message = string .Format( "操作异常,异常消息:{0}。" , exx.Message); } } return result; } |
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/p/delphi7restful_webapi_datacool.html
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?