容联云通讯码短信验证码asp示例

  容联云通讯码短信验证码asp示例,其他语言示例查看官网:http://www.yuntongxun.com/activity/smsDemo#tiyan

  sms.asp,容联云通讯码短信验证码发送asp源代码

其他类库下载

md5.asp

aspJSON1.17.asp

base64.asp

<!--#include file="md5.asp"-->
<!--#include file="aspJSON1.17.asp"-->
<!--#include file="base64.asp"-->
<%
'=========容联云通讯配置开始=========
isXML=true'是否XML数据
accountSid="accountSid配置值"'说明:主账号,登陆云通讯网站后,可在"控制台-应用"中看到开发者主账号ACCOUNT SID。
accountToken="accountToken配置值"'说明:主账号Token,登陆云通讯网站后,可在控制台-应用中看到开发者主账号AUTH TOKEN。
'===========模板配置,注意appId和templateId要一致,要不发送短信会提示错误,更换模板切换这里或者调用SendSMS时传递不同的appId和templateId也行
appId="appId配置值"'说明:应用Id,如果是在沙盒环境开发,请配置"控制台-应用-测试DEMO"中的APPID。如切换到生产环境,请使用自己创建应用的APPID。
templateId="1"'短信模板id,登录后台,管理,短信模板,模板列表里面有。测试模板为1
'=========容联云通讯配置结束=========
'发送http请求
function getHTTPPage(strurl,data,Authorization)
	'set http = Server.CreateObject("Msxml2.XMLHTTP")'放在win2003,win2008服务器上有些会报错,使用下面的组件,同时需要设置setOption,window7测试没有问题这个
	set http = Server.CreateObject("Msxml2.ServerXMLHTTP")'请求
	http.Open "POST",strurl, false 
	http.setOption(2) = 13056'请求的是ssl连接,忽略验证出现的错误。要不可能出现msxml3.dll (0x80072F0D) The certificate authority is invalid or incorrect错误
	Http.setRequestHeader "content-length", len(data)
	Http.setRequestHeader "Authorization", Authorization
    if isXML then'数据格式为xml
      Http.setRequestHeader "Accept", "application/xml;"
      Http.setRequestHeader "Content-type", "application/xml;charset=utf-8;"
    else'数据格式为JSON
	  Http.setRequestHeader "Accept", "application/json;"
      Http.setRequestHeader "Content-type", "application/json;charset=utf-8;"
    end if
	
	http.Send(data)
    response.Write http.responseText
    if isXML then
	  set getHTTPPage=http.responseXML
    else
	  getHTTPPage=http.responseText
    end if
	set http=nothing
end function 
Function rndint(byval MaxNum,byval MinNum)'asp生成整数之间的随机数函数
  dim tmp
  if MaxNum<MinNum then'交换大小值,防止传递时大小搞反
    tmp=MinNum:MinNum=MaxNum:MaxNum=tmp
  end if
  
  Randomize
  rndint=int(rnd *(MaxNum-MinNum+1)+MinNum)
End Function
'短信发送
'arrData:模板中要替换的内容,有多个要替换内容传递多个数组项,先后顺序以模板中定义的替换项“{数字}”中的数字一致
function SendSMS(phone,arrData,appId,templateId)
  dim y,m,d,h,f,s,shijian,sig,Authorization,url,sdata,binfo,o,data
  y=year(now):m=right("0" & month(now), 2):d=right("0" & day(now), 2):h=right("0" & hour(now), 2):f=right("0" & minute(now), 2):s=right("0" & second(now), 2)
  shijian=y&m&d&h&f&s
  sig=ucase( md5(accountSid&accountToken & shijian))
  Authorization = encode64(accountSid&":"& shijian)
  url="https://sandboxapp.cloopen.com:8883/2013-12-26/Accounts/"&accountSid&"/SMS/TemplateSMS?sig=" & sig
  data=""
  for i=0 to ubound(arrData)
    if isXML then
      data=data&"<data><![CDATA["&arrData(i)&"]]></data>"
    else
      data=data&","""&replace(arrData(i),"""","\""")&""""
    end if
   next
  if isXML then
    sdata="<?xml version='1.0' encoding='utf-8'?><TemplateSMS>"&_
    "<to>"&phone&"</to>"&_
    "<appId>"&appId&"</appId>"&_
    "<templateId>"&templateId&"</templateId>"&_
    "<datas>"&data&"</datas>"&_
   "</TemplateSMS>"
  else
    if data<>"" then data=right(data,len(data)-1)
    sdata="{""to"":"""&phone&""",""appId"":"""&appId&""",""templateId"":"""&templateId&""",""datas"":["&data&"]}"
  end if
  if isXML then
    set binfo=getHTTPPage(url,sdata,Authorization)'发送并把结果赋给binfo
    SendSMS=cint(binfo.getelementsbytagname("statusCode").item(0).text)
    set binfo=nothing
  else
    binfo=getHTTPPage(url,sdata,Authorization)'发送并把结果赋给binfo
    set o=new aspJSON
    o.loadJSON(binfo)
    SendSMS=cint(o.data("statusCode"))'返回值大于0都是发失败,自己看这个地址对照错误信息:http://www.yuntongxun.com/activity/errorCode#tiyan
    set o=nothing
  end if
end function
'容联云通讯码短信验证码asp示例
'data="1234"'如果有多个数据,直接用|分隔,如  验证码|验证码有效时间
'code=SendSMS("139xxxxxxxx",split(data,"|"),appId,templateId)
'response.Write code
%>

test.asp。容联云通讯码短信验证码asp示例源代码

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%response.CharSet="utf-8":Session.CodePage=65001 %>
<!--#include file="sms.asp"-->
<%
    if request.Form("op")="sms" then''''''''''''发送短信
       p=request.Form("p")
       if p<>"" then
         smsmsg=""
         for i=0 to 3'4位验证码数字,更加多修改这里的3
           smsmsg=rndint(0,9)&smsmsg
         next
         code=SendSMS(p,split(smsmsg,"|"),appId,templateId)
         response.Clear
         if code=0 then session(p)=smsmsg'发送成功session记录验证码
         response.Write code
       end if
       response.End
    end if
    if request.Form("op")="validphone" then''''''''''''ajax验证输入的号是否正确
       p=request.Form("p")
       code=request.Form("code")
       if session(p)=code then
         response.Clear
         response.Write 0
       else
        response.Write session(p)
       end if
       response.End
    end if
    
 
if Request.QueryString("action")="add" then''''''''''''添加验证
    phone=request.Form("shouji")
    if phone="" then
      response.Write "<script>alert('手机不能为空');history.go(-1)</script>"	
      response.end 
    end if
   '手机验证码开始============================
   phonecode=request.Form("phonecode")
   if phonecode="" then
     response.Write "<script>alert('手机验证码不能为空');history.go(-1)</script>"	
     response.end 
   end if
   if phonecode<>session(phone) then
     response.Write "<script>alert('手机验证码不正确');history.go(-1)</script>"	
     response.end 
   end if
  '手机验证码结束============================
  '===其他验证及入库代码
 end if 
%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>容联云通讯码短信验证码asp示例</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
    function sendMessage(btn) {
        var p = $('#studentMobil');
        if (!/^(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/.test(p.val())) { alert('请输入正确的手机号!'); p.select(); return false; }
        btn.disabled = true;
        var t = 60;
        btn.value = t + 's后重新获取';
        var timer = setInterval(function () {
            t--;
            btn.value = t + 's后重新获取';
            if (t == 0) {
                clearInterval(timer);
                btn.disabled = false;
                btn.value = '发送验证码';
            }
        }, 1000);
        $.ajax({
            url: 'test.asp', data: {op:'sms',p:p.val()}, type: 'POST', complete: function (d) {
                if (parseInt(d.responseText) !== 0) alert('手机验证码发送失败,服务器返回:' + d.responseText)
                else alert('手机验证码发送成功,请注意查看您的手机!');
            }
        });
    }
    function checkPhoneValid(ipt) {
        var p = $('#studentMobil');
        if (!/^(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/.test(p.val())) { alert('请输入正确的手机号!'); p.select(); return false; }
        if (!/^\d+$/.test(ipt.value)) { alert('请输入4位数字!'); ipt.select(); return false }
        $(ipt).next().html('正在验证..');
        $.ajax({
            url: 'test.asp', data: { op: 'validphone', p: p.val(), code: ipt.value }, type: 'POST', complete: function (d) {
                console.log($(ipt))
                if (parseInt(d.responseText) === 0) $(ipt).next().html('<font color=green>验证成功</font>');
                else $(ipt).next().html('<font color=red>验证码错误</font>');
            }
        });
    }
</script>
</head>
<body>
<form action="?action=add" method="post"><table width="100%" border="0" cellspacing="2" cellpadding="2">
 <tr><td align="right">手机号码:</td>
<td><input name="shouji" type="text"  class="knei" id="studentMobil" maxlength="11" />
<input type="button" class="jn_yanzheng" onclick="sendMessage(this);" value="发送验证码"/></td>
</tr><tr>
<td ><div align="right">手机验证码:</div></td>
<td><input name="phonecode" type="text"  class="knei" maxlength="4" onblur="checkPhoneValid(this)" />
<div></div></td>
</tr>
<tr>
<td > </td>
<td><input name="提交" type="submit" class="shu" value="注册" /></td>
</tr>
</table>
</form>
</body>
</html>

 



原创文章,转载请注明出处:容联云通讯码短信验证码asp示例
评论(1)Web开发网
阅读(515)喜欢(0)不喜欢(0)Asp/VBScript