博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用阿里大于接口发短信(Delphi版)
阅读量:4674 次
发布时间:2019-06-09

本文共 5797 字,大约阅读时间需要 19 分钟。

  阿里大于是阿里通信旗下产品,融合了三大运营商的通信能力,提供包括短信、语音、流量直充、私密专线、店铺手机号等个性化服务。每条四分五,价钱还算公道,经老农测试,响应速度非常快,基本上是秒到。官方文档提供了JAVA、.NET、PHP、Python、C/C++、NodeJS 等语言的 Demo,唯独没有 Dephi,但这也不能怪马云,毕竟 Delphi 实在太小众了。

   最近用 Delphi 写个 App,注册用户需要用到手机短信验证,于是找到的阿里大于,使用 Delphi 10.1 berlin 写了个简单的 Demo 并测试通过,现在交出代码:

1 /// 
全能地图(QQ:64445322)
2 /// 3 /// 利用阿里大于接口发短信 4 /// 阿里大于网址:http://www.alidayu.com 5 /// 阿里大于短信接口文档:https://api.alidayu.com/doc2/apiDetail.htm?apiId=25450 6 /// 7 /// TOP分配给应用的AppKey 8 /// AppSecret 9 /// 接收手机号码10 /// 短信签名,传入的短信签名必须是在阿里大于“管理中心-短信签名管理”中的可用签名11 /// 短信模板ID12 /// 短信模板变量,例如:{"code":"1234","product":"alidayu"}13 /// 下发结果消息14 ///
是否成功,True = 成功 ,false = 失败
15 function SendSMS(const AppKey, AppSecret, ReceiveNumber, FreeSignName, TemplateCode, TemplateContent: string; var ResultMsg: string): Boolean;16 17 // 签名算法:http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=118 function MakeSign(const AParams: TStringList; const AppSecret: string): string;19 var20 I: Integer;21 Data: string;22 begin23 // 参数排序24 AParams.Sort;25 26 // 参数拼接27 Data := '';28 for I := 0 to AParams.Count - 1 do29 Data := Data + AParams[I].Replace('=', '');30 31 // HMAC 算法32 Result := THashMD5.GetHMAC(Data, AppSecret).ToUpper;33 end;34 35 var36 HTTP: TNetHTTPClient;37 JsonObject: TJSONObject;38 Params: TStringList;39 Response: string;40 begin41 Result := False;42 43 HTTP := TNetHTTPClient.Create(nil);44 Params := TStringList.Create();45 try46 Params.Values['app_key'] := AppKey;47 Params.Values['format'] := 'json';48 Params.Values['method'] := 'alibaba.aliqin.fc.sms.num.send';49 Params.Values['sign_method'] := 'hmac';50 Params.Values['timestamp'] := FormatDateTime('yyyy-MM-dd HH:mm:ss', Now);51 Params.Values['v'] := '2.0';52 Params.Values['sms_type'] := 'normal';53 Params.Values['sms_free_sign_name'] := FreeSignName;54 Params.Values['rec_num'] := ReceiveNumber;55 Params.Values['sms_template_code'] := TemplateCode;56 Params.Values['sms_param'] := TemplateContent;57 Params.Values['sign'] := MakeSign(Params, AppSecret);58 59 HTTP.ContentType := 'application/x-www-form-urlencoded';60 try61 Response := HTTP.Post('https://eco.taobao.com/router/rest', Params).ContentAsString();62 except63 on E: Exception do64 begin65 ResultMsg := E.Message;66 Exit;67 end;68 end;69 70 JsonObject := TJSONObject.ParseJSONValue(Response) as TJSONObject;71 try72 if JsonObject <> nil then73 begin74 if JsonObject.TryGetValue
('alibaba_aliqin_fc_sms_num_send_response.result.success', ResultMsg) then75 Result := ResultMsg.ToUpper = 'TRUE'76 else if JsonObject.TryGetValue
('error_response.msg', ResultMsg) then77 Result := False;78 end;79 80 finally81 JsonObject.Free;82 end;83 84 finally85 HTTP.Free;86 Params.Free;87 end;88 89 end;

 有不少同学还在使用D7,不知道怎么用,稍微改改就可以了。

1 function SendSMS(const AppKey, AppSecret, ReceiveNumber, FreeSignName, TemplateCode, TemplateContent: string; var ResultMsg: string): Boolean; 2  3   function GetStringMD5(const AInPut: string): string; 4   var 5     MD5: TIdHashMessageDigest5; 6     Digest: T4x4LongWordRecord; 7   begin 8     MD5 := TIdHashMessageDigest5.Create; 9     try10       Digest := MD5.HashValue(AInPut);11       Result := MD5.AsHex(Digest);12     finally13       MD5.Free;14     end;15   end;16 17 // 签名算法:http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=118   function MakeSign(const AParams: TStringList; const AppSecret: string): string;19   var20     I: Integer;21     Data: string;22   begin23     // 参数排序24     AParams.Sort;25     // 参数拼接26     Data := '';27     for I := 0 to AParams.Count - 1 do28       Data := Data + StringReplace(AParams[I], '=', '', [rfReplaceAll]);29     // MD5 算法30     Result := GetStringMD5(AppSecret + Data + AppSecret);31   end;32 33 var34   HTTP: TIdHTTP;35   Params: TStringList;36   Response: string;37   JsonObject: ISuperObject;38 begin39   Result := False;40 41   HTTP := TIdHTTP.Create(nil);42   Params := TStringList.Create();43   try44     Params.Values['app_key'] := AppKey;45     Params.Values['format'] := 'json';46     Params.Values['method'] := 'alibaba.aliqin.fc.sms.num.send';47     Params.Values['sign_method'] := 'md5';48     Params.Values['timestamp'] := FormatDateTime('yyyy-MM-dd HH:mm:ss', Now);49     Params.Values['v'] := '2.0';50     Params.Values['sms_type'] := 'normal';51     Params.Values['sms_free_sign_name'] := UTF8Encode(FreeSignName);52     Params.Values['rec_num'] := ReceiveNumber;53     Params.Values['sms_template_code'] := TemplateCode;54     Params.Values['sms_param'] := UTF8Encode(TemplateContent);55     Params.Values['sign'] := MakeSign(Params, AppSecret);56 57     HTTP.HandleRedirects := True;58     HTTP.Request.AcceptCharSet := 'utf-8';59     HTTP.Request.ContentType := 'application/x-www-form-urlencoded';60     try61       Response := HTTP.Post('http://gw.api.taobao.com/router/rest', Params);62     except63       on E: Exception do64       begin65         ResultMsg := E.Message;66         Exit;67       end;68     end;69 70     JsonObject := SO(Response);71     if JsonObject <> nil then72     begin73       ResultMsg := JsonObject.S['alibaba_aliqin_fc_sms_num_send_response.result.success'];74       if ResultMsg <> '' then75         Result := UpperCase(ResultMsg) = 'TRUE'76       else77       begin78         ResultMsg := JsonObject.S['error_response.msg'];79         Result := False;80       end;81     end;82 83   finally84     HTTP.Free;85     Params.Free;86   end;87 88 end;

 

转载于:https://www.cnblogs.com/rtcmw/p/5797169.html

你可能感兴趣的文章