.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>
在这个过程中,必须引用这个插件,不然是没有效果的:
操作代码就以上的这些,我们实现的效果如下:
输入手机号码,获取验证码,实现短信验证。