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

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

/// <author>全能地图(QQ:64445322)</author>
/// <summary>
/// 利用阿里大于接口发短信
/// 阿里大于网址:http://www.alidayu.com
/// 阿里大于短信接口文档:https://api.alidayu.com/doc2/apiDetail.htm?apiId=25450
/// </summary>
/// <param name="AppKey">TOP分配给应用的AppKey</param>
/// <param name="AppSecret">AppSecret</param>
/// <param name="ReceiveNumber">接收手机号码</param>
/// <param name="FreeSignName">短信签名,传入的短信签名必须是在阿里大于“管理中心-短信签名管理”中的可用签名</param>
/// <param name="TemplateCode">短信模板ID</param>
/// <param name="TemplateContent">短信模板变量,例如:{"code":"1234","product":"alidayu"}</param>
/// <param name="ResultMsg">下发结果消息</param>
/// <returns>是否成功,True = 成功 ,false = 失败</returns>
function SendSMS(const AppKey, AppSecret, ReceiveNumber, FreeSignName, TemplateCode, TemplateContent: string; var ResultMsg: string): Boolean; // 签名算法:http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=1
function MakeSign(const AParams: TStringList; const AppSecret: string): string;
var
I: Integer;
Data: string;
begin
// 参数排序
AParams.Sort; // 参数拼接
Data := '';
for I := to AParams.Count - do
Data := Data + AParams[I].Replace('=', ''); // HMAC 算法
Result := THashMD5.GetHMAC(Data, AppSecret).ToUpper;
end; var
HTTP: TNetHTTPClient;
JO: TJSONObject;
Params: TStringList;
Response: string;
begin
Result := False; HTTP := TNetHTTPClient.Create(nil);
Params := TStringList.Create();
try
Params.Values['app_key'] := AppKey;
Params.Values['format'] := 'json';
Params.Values['method'] := 'alibaba.aliqin.fc.sms.num.send';
Params.Values['sign_method'] := 'hmac';
Params.Values['timestamp'] := FormatDateTime('yyyy-MM-dd HH:mm:ss', Now);
Params.Values['v'] := '2.0';
Params.Values['sms_type'] := 'normal';
Params.Values['sms_free_sign_name'] := FreeSignName;
Params.Values['rec_num'] := ReceiveNumber;
Params.Values['sms_template_code'] := TemplateCode;
Params.Values['sms_param'] := TemplateContent;
Params.Values['sign'] := MakeSign(Params, AppSecret); HTTP.ContentType := 'application/x-www-form-urlencoded';
try
Response := HTTP.Post('https://eco.taobao.com/router/rest', Params).ContentAsString();
except
on E: Exception do
begin
ResultMsg := E.Message;
Exit;
end;
end; JO := TJSONObject.ParseJSONValue(Response) as TJSONObject;
try
if JO <> nil then
begin
if JO.TryGetValue<string>('alibaba_aliqin_fc_sms_num_send_response.result.success', ResultMsg) then
Result := ResultMsg.ToUpper = 'TRUE'
else if JO.TryGetValue<string>('error_response.msg', ResultMsg) then
Result := False;
end; finally
JO.Free;
end; finally
HTTP.Free;
Params.Free;
end; end;

D7的版本

function SendSMS(const AppKey, AppSecret, ReceiveNumber, FreeSignName, TemplateCode, TemplateContent: string; var ResultMsg: string): Boolean;

  function GetStringMD5(const AInPut: string): string;
var
MD5: TIdHashMessageDigest5;
Digest: T4x4LongWordRecord;
begin
MD5 := TIdHashMessageDigest5.Create;
try
Digest := MD5.HashValue(AInPut);
Result := MD5.AsHex(Digest);
finally
MD5.Free;
end;
end; // 签名算法:http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&amp;treeId=1
function MakeSign(const AParams: TStringList; const AppSecret: string): string;
var
I: Integer;
Data: string;
begin
// 参数排序
AParams.Sort;
// 参数拼接
Data := '';
for I := to AParams.Count - do
Data := Data + StringReplace(AParams[I], '=', '', [rfReplaceAll]);
// MD5 算法
Result := GetStringMD5(AppSecret + Data + AppSecret);
end; var
HTTP: TIdHTTP;
Params: TStringList;
Response: string;
JsonObject: ISuperObject;
begin
Result := False; HTTP := TIdHTTP.Create(nil);
Params := TStringList.Create();
try
Params.Values['app_key'] := AppKey;
Params.Values['format'] := 'json';
Params.Values['method'] := 'alibaba.aliqin.fc.sms.num.send';
Params.Values['sign_method'] := 'md5';
Params.Values['timestamp'] := FormatDateTime('yyyy-MM-dd HH:mm:ss', Now);
Params.Values['v'] := '2.0';
Params.Values['sms_type'] := 'normal';
Params.Values['sms_free_sign_name'] := UTF8Encode(FreeSignName);
Params.Values['rec_num'] := ReceiveNumber;
Params.Values['sms_template_code'] := TemplateCode;
Params.Values['sms_param'] := UTF8Encode(TemplateContent);
Params.Values['sign'] := MakeSign(Params, AppSecret); HTTP.HandleRedirects := True;
HTTP.Request.AcceptCharSet := 'utf-8';
HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
try
Response := HTTP.Post('http://gw.api.taobao.com/router/rest', Params);
except
on E: Exception do
begin
ResultMsg := E.Message;
Exit;
end;
end; JsonObject := SO(Response);
if JsonObject <> nil then
begin
ResultMsg := JsonObject.S['alibaba_aliqin_fc_sms_num_send_response.result.success'];
if ResultMsg <> '' then
Result := UpperCase(ResultMsg) = 'TRUE'
else
begin
ResultMsg := JsonObject.S['error_response.msg'];
Result := False;
end;
end; finally
HTTP.Free;
Params.Free;
end; end;

利用阿里大于接口发短信(Delphi版)的更多相关文章

  1. 利用阿里大于实现发送短信(JAVA版)

    本文是我自己的亲身实践得来,喜欢的朋 友别忘了点个赞哦! 最近整理了一下利用阿里大于短信平台来实现发送短信功能. 闲话不多说,直接开始吧. 首先,要明白利用大于发送短信这件事是由两部分组成: 一.在阿 ...

  2. Laravel5中使用阿里大于(鱼)发送短信验证码

    在做用户注册和个人中心的安全管理时,我实现借助第三方短信平台(阿里大于(鱼))在Laravel框架中进行手机验证的设置:阿里大于,是阿里通信旗下优质便捷的云通信服务平台,整合了三大运营商的通信能力,为 ...

  3. 一百一十六:CMS系统之使用阿里大于sdk发送短信验证码

    阿里大于短信平台:https://dysms.console.aliyun.com/dysms.htm#/overview 使用教程:https://blog.csdn.net/qq103189393 ...

  4. 用Python调用华为云API接口发短信

    [摘要] 用Python调用华为云API接口实现发短信,当然能给调用发短信接口前提条件是通过企业实名认证,而且有一个通过审核的短信签名,话不多说,showcode #!/usr/bin/python3 ...

  5. 基于ThinkPHP与阿里大于的PHP短信验证功能

    https://blog.csdn.net/s371795639/article/details/53381274 PHP阿里大鱼短信验证 第一步 登陆阿里大于注册账号,在用户管理中心创建应用,确定A ...

  6. 使用阿里大于平台发送短信验证码java代码实现

    待续..网站app后台还未完成,不能添加签名,短信不能正常发送. Tip: https://help.aliyun.com/document_detail/55284.html?spm=5176.sm ...

  7. android 中调用接口发送短信

    android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...

  8. 谁说程序员不懂浪漫?用Python每天自动给女朋友免费发短信

    前言 之前发过一篇文章,用 Python 制作的给父母天气预报提醒的小工具天气变冷了,给父母制作一个天气提醒小助手,这篇文章我同步到博客上之后,有读者在评论区留言,对于部分微信没有网页版接口,导致无法 ...

  9. PHP阿里大于发短信教程

    PHP阿里大于发短信教程 1 先去控制台 https://www.alidayu.com/center/user/account?spm=a3142.7791109.1999204004.5.ZtBQ ...

随机推荐

  1. Windows MySQL5.7安装和配置

    http://www.leixuesong.cn/category/mysql MySQL5.7是MySQL是最新的MySQL大版本,也是官方认为目前性能最好的.MySQL5.7也有很多改动,很多新的 ...

  2. [RF]怎样用Robot Framework写好Test Case?

    1.介绍 这是一个关于如何用Robot Framework写好Test Case的高层次的指导准则 怎样实际的与系统进行交互不在此文档范围内 最重要的准则是使测试用例尽可能的让熟悉此领域的人觉得简单易 ...

  3. asio的网络通讯代码练手

    asio的网络基本模板(单例模式 消息队列 ) // MyAsio.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include < ...

  4. 如何将service绑入到spring 并且在action中使用

    第一步:定制 service接口,为什么用接口我也不清楚 package com.inspur.services; import com.hsp.domain.User; public interfa ...

  5. SpringMVC 学习 九 SSM环境搭建 (二) Spring配置文件的编写

    spring配置文件中需要干的事情 (一)开启  Service与pojo包的注解扫描 注意:spring 扫描与表对应的实体类,以及service层的类,不能用来扫描Controller层的类,因为 ...

  6. python学习 day21 (3月28日)----(抽象类 多态 nametuple dump)

    不要因为走的路太久了,而忘记了为了什么而出发. 提前作准备了吗?把思维导图的东西做了吗? 和工作了几年的人,相比,是不是相同的水平,如果要写简历的话. 一边学习,一边复习. 小就是大,少就是多. 1. ...

  7. shell for if

    #!/bin/bash ..} do ];then j="${i}" else j="${i}" fi echo $j >> venn.log ec ...

  8. urllib — URL handling modules

    urllib is a package that collects several modules for working with URLs: •urllib.request for opening ...

  9. mysql (_mysql_exceptions.OperationalError) (1055, "Expression #1 of SELECT list is not in GROUP BY clause

    sudo gedit /etc/mysql/my.cnf在打开的my.cnf文件中添加 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 保存,退 ...

  10. error C2065: 'IDD_DIALOG1' : undeclared identifier

    添加资源文件 #include "resource.h"