首页 > 代码库 > delphi7的新生,参与分布式应用开发,调用RESTful API,Json的应用

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的使用:

//提交会员开卡信息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);

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

 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;        }

  

  

  

delphi7的新生,参与分布式应用开发,调用RESTful API,Json的应用