mvc 短信验证发送

        这个是短信验证发送一个例子,你们可以找其他的短信平台。这里只是告诉你们一个使用方法。

第零步:到个短信平台注册,我这个平台是 http://www.ihuyi.com/  先声明没有丝毫的打广告的意思,因为它有免费的短信可以                  使用。

第一步:首先是先去下图这 add  开头的2个链接 复制到你项目的Web.config文件中就行了。

 <configuration>
    <appSettings>
        <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"/>
    </appSettings>
    <connectionStrings/>

 第二步:构建发送号码的html,这里随便发挥,只要把号码mobile这个传过来就行。

 第三步:逻辑层接收传过来的参数(号码),然后把   APIID  APIKEY  修改一下。

               验证码的随机数也是可以限制它的范围,我这里是100000到999999之间。

  //信息发送
        public static string PostUrl = ConfigurationManager.AppSettings["WebReference.Service.PostUrl"];
        public ActionResult send_new(string mobile)
        {

            string account = "APIID";//用户名是登录用户中心->验证码、通知短信->帐户及签名设置->APIID
            string password = "APIKEY"; //密码是请登录用户中心->验证码、通知短信->帐户及签名设置->APIKEY
           
            Random rad = new Random();
            int mobile_code = rad.Next(100000, 999999);//验证码的随机数
            string content = "您的验证码是:" + 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);
            }

        }

 

       这个平台也可以使用发送信息,你们也可以直接去平台哪里下载一个说明书之类的文件里面有详细的资料和其他的语言。

小编能力不足只是做了MVC的信息发送,这个是要连接网络才可以发送消息,它只是一个代替你发送信息的平台。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值