前幾天作一個asp語言開發的網站須要實現用戶註冊短信驗證功能,就研究了一下如何實現,簡單給你們分享下調用過程。
首先須要找到一個第三方短信接口,當時用的是動力思惟樂信的短信接口。
首先須要先註冊個動力思惟樂信平臺帳號,這個後面要用到,能夠到他們官網註冊(http://www.lx598.com/)
這裏貼出來他們的短信接口接入文檔說明(http://www.lx598.com/apitext.html),和asp語言的demo(http://www.lx598.com/aspCode.html)
前端用戶註冊頁面(這個比較簡單)html
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <!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>發送短信</title> </head> <body> <hr /> <form name="form1" action="index4.asp" method="post" > 發送短信<br /> 用戶名: <input name="AccountName" type="text" /> 密碼: <input name="Password" type="password" /> <br /> 內容: <input name="message" type="text" /> 目標號碼: <input name="mobiles" type="text" /> <input name="submit"type="submit" value="提交" /> <input name="reset"type="reset" value="重置" /> <input name="cd" type="hidden" value="4" /> <input name="batchNumber" type="hidden" value="<%=now()%>" /> </form> </body> </html>
後臺代碼前端
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <!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>發送短信</title> </head> <body> <!--#include file="md5.asp"--> <% Function BytesToBstr(body,code) dim objstream set objstream = Server.CreateObject("adodb.stream") objstream.Type = 1 objstream.Mode =3 objstream.Open objstream.Write body objstream.Position = 0 objstream.Type = 2 objstream.Charset =code BytesToBstr = objstream.ReadText objstream.Close set objstream = nothing End Function %> <% 'response.ContentType="text/xml;charset=UTF-8" dim cdkey1,password1,cd dim HttpReq,str,url dim cpname,cpadrr,cplinkman,cpphone,cpmobile,cpfax,cpmail,cppost,cpworks,manager cd=request.Form("cd") cmd=request.Form("cmd") cdkey1=request.Form("AccountName") password1=UCase(md5(request.Form("password"))) cpname=request.Form("cpname") cpadrr=request.Form("cpadrr") cplinkman=request.Form("cplinkman") cpphone=request.Form("cpphone") cpmobile=request.Form("cpmobile") cpfax=request.Form("cpfax") cpmail=request.Form("cpmail") cppost=request.Form("cppost") cpworks=request.Form("cpworks") manager=request.Form("manager") url="cmd="+cmd+"&AccountName="+cdkey1+"&Password="+password1 if cd="1" then Set HttpReq = Server.CreateObject("Microsoft.XMLHTTP") HttpReq.open "post", "http://116.255.135.146/WebClient", False,"","" HttpReq.setRequestHeader "Content-Length",len(url) HttpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" HttpReq.send(url) 'str=HttpReq.responseXML if inStr(BytesToBstr(HttpReq.responseBody,"UTF-8"),"<") then Set XMLDOC = Server.CreateObject("Microsoft.XMLDOM") XMLDOC.async = false XMLDOC.loadxml(BytesToBstr(HttpReq.responseBody,"UTF-8")) set XMLDOCElement=XMLDOC.documentElement dimResultStr1=XMLDOCElement.SelectSingleNode("//reply/ErrorCode").text set objnodes=Nothing Set XMLDOC = Nothing else dimResultStr1=dimThisSmsState1 end if if dimResultStr1=0 then response.write "發送成功" else response.write "發送失敗" end if 'response.Write str end if %> <br /> <% Function UrlEncoding(DataStr) Dim StrReturn,Si,ThisChr,InnerCode,Hight8,Low8 StrReturn = "" For Si = 1 To Len(DataStr) ThisChr = Mid(DataStr,Si,1) If Abs(Asc(ThisChr)) < &HFF Then StrReturn = StrReturn & ThisChr Else InnerCode = Asc(ThisChr) If InnerCode < 0 Then InnerCode = InnerCode + &H10000 End If Hight8 = (InnerCode And &HFF00)\ &HFF Low8 = InnerCode And &HFF StrReturn = StrReturn & "%" & Hex(Hight8) & "%" & Hex(Low8) End If Next UrlEncoding = StrReturn End Function %> <% if cd="4" then mobiles=request.Form("mobiles") content=request.Form("message") cmd1=request.Form("cmd") batch=replace(request.Form("batchNumber"),"-","0") batchNumber=replace(batch,":","") batchNumber=replace(batchNumber," ","") batchNumber=left(batchNumber,15) url="accName="+cdkey1+"&accPwd="+password1+"&aimcodes="+mobiles+"&content="+content+"【簽名】 Set HttpReq = Server.CreateObject("Microsoft.XMLHTTP") HttpReq.open "post", "http://www.lx198.com/sdk/send", False,"","" HttpReq.setRequestHeader "Content-Length",len(url) HttpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" url=URLEncoding(url) HttpReq.send(url) str=HttpReq.responseText response.Write str end if %> </body> </html>