.NET短信接口驗證
之前遇到的一個問題,因為沒有接觸過,所以自己上網查閱過資料也向他人請教以及老師,.NET短信接口調用,其實,網上有許多免費的短信接口平臺,但也是有限度的,如果發送的數量過多,我們也可以購買,我這里數量不多,只需要用免費的就可以了,首先,我們必須要注冊一個賬戶,獲得使用的權限,在這里,我用的是互億無線短信平臺,也可以根據個人需要選擇。
我們需要把樣式寫好:
<div class="col-lg-2 col-md-2 col-sm-2 text-right"><span class="control-label">手機號碼</span>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 reset"><div class="col-sm-6" style="margin-left:-15px;"><input type="text"style="width:180px; height:34px;" class="form-control" id="Cellphone" name="Cellphone" onblur="fff()"/></div><div class="col-sm-6" style="margin-left:62px;width:95px!important"><input type="text" class="form-control" style="width:80px; height:34px"/></div>
</div><div class="col-lg-1" style="margin-left:-31px;"><button class="btn btn-success" type="button" id="zphone" onClick="mobile();">獲取驗證碼</button>
</div>
我們要檢查我們在Web.config中有沒有給到鏈接:
如果沒有,我們需要在控制器中給到鏈接:
這里我們可以自主選擇方法。
接下來我們需要在控制器里面寫方法:
public ActionResult dx(string mobile){string account = "用戶名/";//用戶名是登錄用戶中心->驗證碼、通知短信->帳戶及簽名設置->APIIDstring password = "密碼"; //密碼是請登錄用戶中心->驗證碼、通知短信->帳戶及簽名設置->APIKEYmobile = "手機號碼";Random rad = new Random();int mobile_code = rad.Next(1000, 10000);string content = "您的驗證碼是:" + mobile_code + " 。請不要把驗證碼泄露給其他人。";Session["mobile"] = mobile;//Session["mobile_code"] = mobile_code;//string postStrTpl = "account={0}&password={1}&mobile={2}&content={3}";UTF8Encoding encoding = new UTF8Encoding();byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content));HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);myRequest.Method = "POST";myRequest.ContentType = "application/x-www-form-urlencoded";myRequest.ContentLength = postData.Length;Stream newStream = myRequest.GetRequestStream();// Send the data.newStream.Write(postData, 0, postData.Length);newStream.Flush();newStream.Close();HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();if (myResponse.StatusCode == HttpStatusCode.OK){StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);//Response.Write(reader.ReadToEnd());string res = reader.ReadToEnd();int len1 = res.IndexOf("</code>");int len2 = res.IndexOf("<code>");string code = res.Substring((len2 + 6), (len1 - len2 - 6));//Response.Write(code);int len3 = res.IndexOf("</msg>");int len4 = res.IndexOf("<msg>");string msg = res.Substring((len4 + 5), (len3 - len4 - 5));Response.Write(msg);Response.End();return Json(msg, JsonRequestBehavior.AllowGet);}else{//訪問失敗return Json("", JsonRequestBehavior.AllowGet);}}
在寫完控制器之后,我們要寫一個方法來調用,提交發送短信驗證:
<script language="javascript">function mobile() {var Cellphone = $('#Cellphone').val();$.post("/Aiyumye/BasicInformation/dx", { Cellphone: Cellphone }, function (msg) {if (msg == '提交成功') {RemainTime();}});};var iTime = 59;var Account;function RemainTime(){document.getElementById('zphone').disabled = true;var iSecond,sSecond="",sTime="";if (iTime >= 0){iSecond = parseInt(iTime%60);iMinute = parseInt(iTime/60)if (iSecond >= 0){if(iMinute>0){sSecond = iMinute + "分" + iSecond + "秒";}else{sSecond = iSecond + "秒";}}sTime=sSecond;if(iTime==0){clearTimeout(Account);sTime='獲取手機驗證碼';iTime = 59;document.getElementById('zphone').disabled = false;}else{Account = setTimeout("RemainTime()",1000);iTime=iTime-1;}}else{sTime='沒有倒計時';}document.getElementById('zphone').value = sTime;}</script>
在這個過程中,必須引用這個插件,不然是沒有效果的:
操作代碼就以上的這些,我們實現的效果如下:
輸入手機號碼,獲取驗證碼,實現短信驗證。