C# (MVC) SMS interface

Title C# (MVC) SMS Interface

Development tools and key technologies: Visual Studio, SQL, C# MVC,
Author: Liu Dongbiao
Date of writing: January 18, 2019
Reference material Huyi wireless SMS platform address: http://www.ihuyi.com/

When making the SMS interface, we need to pull in the following interface and put it in Web.config

<add key="WebReference.Service.PostUrl" value="http://106.ihuyi.cn/webservice/sms.php?method=Submit"/>
<add key="WebReference.sms" value="http://106.ihuyi.cn/webservice/sms.php?smsService"/>

insert image description here

、、、、、、、、、、Controller控制器代码、、、、、、、、、、、
#region 短信接口
        public static string PostUrl = ConfigurationManager.AppSettings["WebReference.Service.PostUrl"];

        public ActionResult send_new(string mobile)
        {
            string account = "C85390283";//用户名是登录用户中心->验证码、通知短信->帐户及签名设置->APIID
            string password = "9fdf97c8dc894605994a370b97ee534c"; //密码是请登录用户中心->验证码、通知短信->帐户及签名设置->APIKEY
                                          //string mobile = Request.QueryString["mobile"];

            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);
            }

        }
        #endregion

Through the Session["mbile_code"] mobile verification code automatically generated above, compare it with the verification code received by the mobile phone

  //手机号码认证
        public ActionResult UpdatePhone(int UserID,string Cellphone,int ceshi)
        {
            string strMsg = "手机号码认证失败";
            try
            {
                var number = Convert.ToInt32(Session["mobile_code"]);
                var list = (from tbCellphone in myModel.S_Cellphone
                            join tbClient in myModel.S_Client on tbCellphone.ClientID equals tbClient.ClientID
                            where tbClient.UserID == UserID
                            select tbCellphone).Single();
                if (list.Cellphone == Cellphone)
                {
                    strMsg = "手机号码认证失败,相同的手机号码";
                }
                else if (number != ceshi)
                {
                    strMsg = "验证码错误,请重新输入!";
                } else if(list.CheckState== true)
                {
                    list.Cellphone = Cellphone;
                    list.CellphoneDate = DateTime.Now;
                    myModel.Entry(list).State = System.Data.Entity.EntityState.Modified;
                    myModel.SaveChanges();
                    strMsg = "手机号码认证成功";
                }
                else
                {
                    strMsg = "手机号码认证正在审核,请耐心等待!";
                }
                
            }
            catch (Exception)
            {

               return Json("个人资料未完善,请前去填写!", JsonRequestBehavior.AllowGet);
            }
            return Json(strMsg, JsonRequestBehavior.AllowGet);
        }

,,,,,,,,,,,,,,,, js code,,,,,,,,,,,,,

  //修改手机号码
        $("#SubmitPhone").click(function () {
            var ceshi = $("#ceshi").val();
            var mobile = $("#mobile").val();
            var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-3]{1})|(15[4-9]{1})|(18[0-9]{1})|(199))+\d{8})$/;
            if (mobile.length != 11 || !myreg.test(mobile)) {
                layer.msg("请填写11位有效的手机号码");
            } else if (ceshi == "") {
                layer.msg("请填写验证码");
            } else {
                $.post("/MyAccount/MyAccount/UpdatePhone", { UserID: UserID, Cellphone: mobile, ceshi: ceshi }, function (data) {
                    if (data == "手机号码认证成功") {
                        layer.alert(data, { icon: 6, title: '提示' });
                        $.post("/MyAccount/MyAccount/SelectClient", { UserID: UserID }, function (data) {
                            $("#ClientPhones").text(data[0].Cellphone);
                            $(".btn_five").addClass("hide");
                            $(".btn_frle").removeClass("hide");
                        })
                    } else {
                        layer.alert(data, { icon: 6, title: '提示' });
                    }

                })
            }
        })
        //短信接口
        function get_mobile_code() {
            var modile = $("#mobile").val();
            var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-3]{1})|(15[4-9]{1})|(18[0-9]{1})|(199))+\d{8})$/;
            if (modile == "") {
                layer.msg("请输入手机号码");
            } else if ($("#mobile").val().length != 11) {
                layer.msg("请输入11位的手机号码");
            } else if (!myreg.test(modile)) {
                layer.msg("请输入有效的手机号码");
            } else {
                $.post("/MyAccount/MyAccount/send_new", { mobile: modile }, function (data) {
                    if (data == "提交成功") {
                        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;
        }

Guess you like

Origin blog.csdn.net/weixin_44538423/article/details/86542802