使用阿里云平台发送短信

我们提供了两个短信发送平台的示例,通过阿里云平台发送,使用上复杂一些,且申请过程需要一个已经备案的域名,第三方平台使用简单,不需要已备案的域名即可申请,但使用阿里云平台 功能更多,加密性也要好一些。

首先做好以下几步:

1、 按照这里的说明购买和设置短信服务:

https://help.aliyun.com/document_detail/55288.html?spm=a2c4g.11186623.6.560.3d4065f4vhQzEK
里面有提供了100次的免费试用
 

2、 按照这里的方式添加访问凭证AccessKey和凭证密钥AccessKeySecret 

https://next.api.aliyun.com/api-tools/demo/Dysmsapi/db7e1211-14e0-4b7b-9011-037dfb85d42e
 

3、 登录阿里云接入控制台,打开短信服务,首先先创建签名和模板才能使用。方法参考开发文档:

https://help.aliyun.com/document_detail/55288.html?spm=a2c4g.11186623.6.560.6b1037d5DPC209

 

以下是开发过程和参考代码:
 

 

到全局代码添加下面代码:

 

Public Function PercentEncode(ByVal value As String) As String

    Dim stringBuilder As StringBuilder = New StringBuilder()

    Dim text As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"

    Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(value)

    For Each b As Byte In bytes

        Dim c As Char = chr(b)

        If text.IndexOf(c) >= 0 Then

            stringBuilder.Append(c)

        Else

            stringBuilder.Append("%").Append(String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:X2}", CInt(b)))

        End If

    Next

    Return stringBuilder.ToString()

End Function

 

Public Function SignString(ByVal accessSecret As String,ByVal source As String) As String

    Dim AccessKeySecret As String = accessSecret & "&"

    Dim sign1 As String = "GET&%2F&" & PercentEncode(source)

    Dim signRet As String

    using hmac As System.Security.Cryptography.KeyedHashAlgorithm = System.Security.Cryptography.KeyedHashAlgorithm.Create("HMACSHA1")

        hmac.key = System.Text.Encoding.UTF8.GetBytes(AccessKeySecret.ToCharArray())

        Dim hashValue() As Byte = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(sign1.ToCharArray))

        signRet = Convert.ToBase64String(hashValue)

    End using

    Return signRet

End Function

 

给指定号码发送短信:

 

可以同时给多个号码发送,只能使用一个签名和模板,所有号码接收的是同样内容的短信。

 

'定义一个可排序的字典

Dim lst As new SortedDictionary(of String,String)(StringComparer.Ordinal)

lst.Add("AccessKeyId","123456789") 'AccessKey凭证id

lst.Add("Format","JSON") '返回的结果格式

lst.Add("Action","SendSms") '调用的接口

lst.Add("RegionId","cn-hangzhou") '

lst.Add("SignatureMethod","HMAC-SHA1") '激活模式

lst.Add("SignatureNonce",System.Guid.NewGuid().ToString()) '随机数

lst.Add("SignatureVersion","1.0") '签名版本

lst.Add("Timestamp",PercentEncode(format(DateTime.UtcNow,"yyyy-MM-ddTHH:mm:ssZ"))) 'Utc时间

lst.Add("Version","2017-05-25") '界面版本

lst.Add("PhoneNumbers", PercentEncode("15500000000")) '的话号码,

'如果是多个号码参考:lst.Add("PhoneNumbers", PercentEncode("15500000000,13000000000"))

lst.Add("SignName", PercentEncode("Foxtable")) '签名

lst.Add("TemplateParam", PercentEncode("{""sfno"":""9999999""}")) '模板参数sfno为模板变量,9999999为变量值

lst.Add("TemplateCode", "SMS_000000000") '模板.

'按顺序拼接参数

Dim su As new StringBuilder

For Each key As String In lst.keys

    su.AppendFormat("&{0}={1}",key,lst(key))

Next

Dim s As String =su.ToString().trimstart("&")

Dim AccessKeySecret  As String = "987654321" '凭证密钥

Dim sign As String = SignString(AccessKeySecret,s) '加密参数字符串

'拼接接口url

Dim url As String = "http://dysmsapi.aliyuncs.com/?Signature=" & PercentEncode(sign) & "&" & s

'Dim urls As String = "https://dysmsapi.aliyuncs.com/?Signature=" & PercentEncode(sign) & "&" & s

Dim hc As New HttpClient(url)

Dim ret As String = hc.GetData()

MessageBox.Show(ret)

 

批量发送短信:

 

可以同时给多个号码发送,每个号码都可以使用不同的签名和指定模板里不同的变量值,模板都是同一个。

 

'定义一个可排序的字典

Dim lst As new SortedDictionary(of String,String)(StringComparer.Ordinal)

lst.Add("AccessKeyId","123456789") 'AccessKey

lst.Add("Format","JSON") '返回的结果格式

lst.Add("Action","SendBatchSms") '调用的接口

lst.Add("RegionId","cn-hangzhou") '

lst.Add("SignatureMethod","HMAC-SHA1") '激活模式

lst.Add("SignatureNonce",System.Guid.NewGuid().ToString()) '随机数

lst.Add("SignatureVersion","1.0") '签名版本

lst.Add("Timestamp",PercentEncode(format(DateTime.UtcNow,"yyyy-MM-ddTHH:mm:ssZ"))) 'Utc时间

lst.Add("Version","2017-05-25") '界面版本

lst.Add("PhoneNumberJson", PercentEncode("[""15500000000"",""13300000000""]")) '指定的不同手机号

lst.Add("SignNameJson", PercentEncode("[""签名1"",""签名2""]")) '指定的不同签名

lst.Add("TemplateParamJson", PercentEncode("[{""sfno"":""变量值1""},{""sfno"":""变量值2""}]")) '指定的不同变量值

lst.Add("TemplateCode", "SMS_000000000") '指定短信模板.

'按顺序拼接参数

Dim su As new StringBuilder

For Each key As String In lst.keys

    su.AppendFormat("&{0}={1}",key,lst(key))

Next

Dim s As String =su.ToString().trimstart("&")

Dim AccessKeySecret  As String = "987654321" '凭证密钥

Dim sign As String = SignString(AccessKeySecret,s) '加密参数 

'Dim url As String = "http://dysmsapi.aliyuncs.com/?Signature=" & PercentEncode(sign) & "&" & s

Dim urls As String = "https://dysmsapi.aliyuncs.com/?Signature=" & PercentEncode(sign) & "&" & s

Dim hc As New HttpClient(urls)

Dim ret As String = hc.GetData()

MessageBox.Show(ret)

 

查询短信发送记录:

 

'定义一个可排序的字典

Dim lst As new SortedDictionary(of String,String)(StringComparer.Ordinal)

lst.Add("AccessKeyId","123456789") 'AccessKey

lst.Add("Format","JSON") '返回的结果格式

lst.Add("Action","QuerySendDetails") '调用的接口

lst.Add("RegionId","cn-hangzhou") '

lst.Add("SignatureMethod","HMAC-SHA1") '加密模式

lst.Add("SignatureNonce",System.Guid.NewGuid().ToString()) '随机数

lst.Add("SignatureVersion","1.0") '签名版本

lst.Add("Timestamp",PercentEncode(format(DateTime.UtcNow,"yyyy-MM-ddTHH:mm:ssZ"))) 'Utc时间

lst.Add("Version","2017-05-25") '界面版本

lst.Add("PhoneNumber", PercentEncode("15500000000")) '电话号码

lst.Add("CurrentPage", 1) '查询第几页

lst.Add("PageSize", 10) '查询每页记录数

lst.Add("SendDate", PercentEncode(Format(Date.Today,"yyyyMMdd"))) '查询短信发送日期

Dim su As new StringBuilder

For Each key As String In lst.keys

    su.AppendFormat("&{0}={1}",key,lst(key))

Next

Dim s As String =su.ToString().trimstart("&")

Dim AccessKeySecret As String = "87654321"

Dim sign As String = SignString(AccessKeySecret,s)

Dim url As String = "http://dysmsapi.aliyuncs.com/?Signature=" & PercentEncode(sign) & "&" & s

'Dim urls As String = "https://dysmsapi.aliyuncs.com/?Signature=" & PercentEncode(sign) & "&" & s

Dim hc As New HttpClient(url)

Dim ret As String = hc.GetData()

MessageBox.Show(ret)


本页地址:http://www.foxtable.com/webhelp/topics/3389.htm