以下是一个使用ASP语言调用阿里云短信接口的简单示例代码:
<%
' 引入必要的组件
Set xmlhttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument")
Set xmlElem = Server.CreateObject("MSXML2.XMLDOM")
xmlDoc.async = False
' 设置阿里云短信接口的请求参数
accessKeyId = "你的阿里云AccessKey ID"
accessKeySecret = "你的阿里云AccessKey Secret"
signatureMethod = "HMAC-SHA1"
signatureNonce = CStr(Int(Rnd * 10000000))
signatureVersion = "1.0"
timestamp = URLEncode(FormatDateTime(Now, vbGeneralDate))
' 构建签名字符串
canonicalizedQueryString = "AccessKeyId=" & accessKeyId & "&Format=XML&" & _
"SignatureMethod=" & URLEncode(signatureMethod) & "&" & _
"SignatureNonce=" & URLEncode(signatureNonce) & "&" & _
"SignatureVersion=" & URLEncode(signatureVersion) & "&" & _
"Timestamp=" & timestamp & "&" & _
"Version=2017-05-25"
stringToSign = "GET" & "&" & URLEncode("/") & "&" & URLEncode(canonicalizedQueryString)
' 使用AccessKey Secret生成签名
signature = URLEncode(Base64Encode(HmacSHA1(accessKeySecret & "&", stringToSign)))
' 构建请求URL
requestURL = "http://dysmsapi.aliyuncs.com/?" & canonicalizedQueryString & "&" & "Signature=" & signature
' 发起GET请求,获取短信发送结果
xmlhttp.open "GET", requestURL, False
xmlhttp.send ""
responseXML = xmlhttp.responseText
' 解析XML响应结果,提取短信发送状态码
xmlDoc.loadXML responseXML
Set nodeList = xmlDoc.documentElement.getElementsByTagName("Code")
If nodeList.Length > 0 Then
statusCode = nodeList.Item(0).Text
If statusCode = "OK" Then
' 短信发送成功
Response.Write "短信发送成功!"
Else
' 短信发送失败
Response.Write "短信发送失败,错误码:" & statusCode
End If
Else
Response.Write "解析短信发送结果失败。"
End If
' 函数:将字符串进行URL编码
Function URLEncode(ByVal str)
Dim i, c, strResult
strResult = ""
For i = 1 To Len(str)
c = Mid(str, i, 1)
If Asc(c) >= 48 And Asc(c) <= 57 Or Asc(c) >= 65 And Asc(c) <= 90 Or Asc(c) >= 97 And Asc(c) <= 122 Then
strResult = strResult & c
Else
strResult = strResult & "%" & Right("0" & Hex(Asc(c)), 2)
End If
Next
URLEncode = strResult
End Function
' 函数:使用HMAC-SHA1算法进行加密
Function HmacSHA1(ByVal key, ByVal data)
Set typeobj = Server.CreateObject("System.Text.UTF8Encoding")
Set hmacsha1 = Server.CreateObject("System.Security.Cryptography.HMACSHA1")
Set keyBytes = typeobj.GetBytes_4(key)
hmacsha1.Key = keyBytes
Set dataBytes = typeobj.GetBytes_4(data)
Set hashBytes = hmacsha1.ComputeHash_2(dataBytes)
HmacSHA1 = ByteArrayToHexString(hashBytes)
End Function
' 函数:将字节数组转换为十六进制字符串
Function ByteArrayToHexString(ByVal bytes)
Dim hexstr, i
hexstr = ""
For i = 1 To LenB(bytes)
hexstr = hexstr & Right("0" & Hex(Ascb(Midb(bytes, i, 1))), 2)
Next
ByteArrayToHexString = hexstr
End Function
' 函数:将字符串进行Base64编码
Function Base64Encode(ByVal data)
Set base64Encode = Server.CreateObject("System.Text.UTF8Encoding")
Set base64Encode = Server.CreateObject("System.Convert")
Set bytes = base64Encode.FromBase64String_2(data)
Base64Encode = base64Encode.GetString_4(bytes)
End Function
%>
请将上述代码中的accessKeyId
和accessKeySecret
替换为你自己的阿里云AccessKey ID和AccessKey Secret。
这里仅提供了一个简单的示例代码,实际使用时可能需要根据自己的需求调整和完善代码。同时,为了确保代码的安全性和稳定性,建议在生产环境中使用HTTPS协议进行请求。
德州阿里云代理商提供了ASP短信接口代码,你可以参考以下示例代码:
- 发送短信接口:
<%
' 导入必要的命名空间
Imports System.Security.Cryptography
Imports System.Text
Imports System.Web
Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports System.Net.Http.Headers
' 发送短信函数
Function SendSMS(ByVal mobile As String, ByVal message As String) As String
Dim res As String = ""
Dim param As String = ""
Dim code As String = ""
Dim postDataStr As String = ""
Dim accountSid As String = "your_account_sid" ' 替换为你的账号SID
Dim authToken As String = "your_auth_token" ' 替换为你的授权令牌
Dim url As String = "https://dysmsapi.aliyuncs.com/2017-05-25/SendSms"
' 构建时间戳
Dim epoch As DateTime = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
Dim timestamp As Long = (DateTime.UtcNow - epoch).TotalMilliseconds
' 构建签名
Dim sign As String = AccountSign(accountSid, authToken, timestamp)
' 构建请求参数
param = "{""PhoneNumbers"":""" & mobile & """,""SignName"":""your_sign_name"",""TemplateCode"":""your_template_code"",""TemplateParam"":""{""message"":""" & message & """}""}"
postDataStr = "SignatureMethod=HMAC-SHA1&SignatureNonce=1234567890&SignatureVersion=1.0&AccessKeyId=" & accountSid & "&Timestamp=" & timestamp & "&Format=JSON&Signature=" & HttpUtility.UrlEncode(sign) & "&Action=SendSms&Version=2017-05-25" & "&RegionId=cn-hangzhou&" & param
' 发送请求
Dim result As String = HttpPost(url, postDataStr)
' 处理返回结果
If result.Contains("""Code"":""OK""") Then
res = "短信发送成功"
Else
' 解析错误信息
Dim start As Integer = result.IndexOf("""Code"":""") + 9
Dim [end] As Integer = result.IndexOf("""", start)
code = result.Substring(start, [end] - start)
res = "短信发送失败,错误码:" & code
End If
Return res
End Function
' 构建请求签名的函数
Function AccountSign(ByVal accountSid As String, ByVal authToken As String, ByVal timestamp As Long) As String
Dim signRef As String = accountSid & authToken & timestamp.ToString()
' 使用SHA1哈希算法进行散列计算
Dim sha As SHA1 = New SHA1CryptoServiceProvider()
Dim signBytes As Byte() = sha.ComputeHash(Encoding.UTF8.GetBytes(signRef))
' 将字节数组转换为Base64字符串
Dim sign As String = Convert.ToBase64String(signBytes)
Return sign
End Function
' 发送HTTP POST请求的函数
Function HttpPost(ByVal url As String, ByVal postDataStr As String) As String
Dim request As WebRequest = WebRequest.Create(url)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
Dim parameterBytes As Byte() = Encoding.UTF8.GetBytes(postDataStr)
request.ContentLength = parameterBytes.Length
Dim requestStream As Stream = request.GetRequestStream()
requestStream.Write(parameterBytes, 0, parameterBytes.Length)
requestStream.Close()
Dim response As WebResponse = request.GetResponse()
Dim responseStream As Stream = response.GetResponseStream()
Dim streamReader As StreamReader = New StreamReader(responseStream, Encoding.GetEncoding("utf-8"))
Dim result As String = streamReader.ReadToEnd()
streamReader.Close()
responseStream.Close()
response.Close()
Return result
End Function
' 测试短信发送
Dim mobile As String = "18012345678" ' 接收短信的手机号码
Dim message As String = "您的验证码是:123456" ' 短信内容
Dim result As String = SendSMS(mobile, message)
Response.Write("发送结果:" & result)
%>
请注意替换代码中的your_account_sid
、your_auth_token
、your_sign_name
和your_template_code
为你的阿里云账号对应的信息。
- 查询短信发送记录接口:
<%
' 导入必要的命名空间
Imports System.Security.Cryptography
Imports System.Text
Imports System.Web
Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports System.Net.Http.Headers
' 查询短信发送记录函数
Function QuerySMS(ByVal mobile As String, ByVal date As Date) As String
Dim res As String = ""
Dim param As String = ""
Dim postDataStr As String = ""
Dim accountSid As String = "your_account_sid" ' 替换为你的账号SID
Dim authToken As String = "your_auth_token" ' 替换为你的授权令牌
Dim url As String = "https://dysmsapi.aliyuncs.com/2017-05-25/QuerySendDetails"
' 构建时间戳
Dim epoch As DateTime = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
Dim timestamp As Long = (DateTime.UtcNow - epoch).TotalMilliseconds
' 构建签名
Dim sign As String = AccountSign(accountSid, authToken, timestamp)
' 构建请求参数
param = "{""PhoneNumber"":""" & mobile & """,""SendDate"":""" & date.ToString("yyyyMMdd") & """,""PageSize"":""10"",""CurrentPage"":""1""}"
postDataStr = "SignatureMethod=HMAC-SHA1&SignatureNonce=1234567890&SignatureVersion=1.0&AccessKeyId=" & accountSid & "&Timestamp=" & timestamp & "&Format=JSON&Signature=" & HttpUtility.UrlEncode(sign) & "&Action=QuerySendDetails&Version=2017-05-25&RegionId=cn-hangzhou&" & param
' 发送请求
Dim result As String = HttpPost(url, postDataStr)
' 处理返回结果
If result.Contains("""Code"":""OK""") Then
res = "查询成功"
Else
' 解析错误信息
Dim start As Integer = result.IndexOf("""Code"":""") + 9
Dim [end] As Integer = result.IndexOf("""", start)
code = result.Substring(start, [end] - start)
res = "查询失败,错误码:" & code
End If
Return res
End Function
' 测试短信发送记录查询
Dim mobile As String = "18012345678" ' 查询短信记录的手机号码
Dim date As Date = DateTime.Now.Date ' 查询的日期
Dim result As String = QuerySMS(mobile, date)
Response.Write("查询结果:" & result)
%>
同样需要替换代码中的your_account_sid
和your_auth_token
为你的阿里云账号对应的信息。
以上代码可以帮助你在ASP中使用阿里云短信接口发送短信和查询短信发送记录。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/110273.html