码迷,mamicode.com
首页 > Web开发 > 详细

.net 实现之短信验证码

时间:2017-05-29 16:38:24      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:ice   config   bre   false   cond   settings   代码   code   word   

接口类型:互亿无线触发短信接口,支持发送验证码短信、订单通知短信等。

账户注册:请通过该地址开通账户http://sms.ihuyi.com/register.html

只能测试用:

  实现注册页面

     

<script type="text/javascript">
    function get_mobile_code() {
        $.get(‘Post.aspx‘, { mobile: jQuery.trim($(‘#mobile‘).val()) }, function (msg) {
            alert(jQuery.trim(unescape(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>

  

<form action="" method="post" name="formUser" onSubmit="return register();">
    <table width="100%" border="0" align="left" cellpadding="5" cellspacing="3">
        <tr>
        <td align="right">手机<td>
        <input id="mobile" name="extend_field5" type="text" size="25" class="inputBg" /><span style="color:#FF0000"> *</span> 
        <input id="zphone" type="button" value=" 发送手机验证码 " onClick="get_mobile_code();"></td>
        </tr>
        <tr>
            <td align="right">验证码</td>
            <td><input type="text" size="8" name="captcha" class="inputBg" /></td>
        </tr>
    </table>
</form>

后台代码

 

 public static string PostUrl = ConfigurationManager.AppSettings["WebReference.Service.PostUrl"];
        protected void Page_Load(object sender, EventArgs e)
        {
            string account = "C23795760";//用户名是登录用户中心->验证码、通知短信->帐户及签名设置->APIID
            string password = "b79bc3ff3985ea849964fb7a5fdf78ea"; //密码是请登录用户中心->验证码、通知短信->帐户及签名设置->APIKEY
            string mobile = Request.QueryString["mobile"];
            Random rad = new Random();
            int mobile_code = rad.Next(1000, 10000);
            string content = "我就是来测试的别害怕zmd:" + 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();

            }
            else
            {
                //访问失败
            }
        }

web.confg

 <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>

 

     

.net 实现之短信验证码

标签:ice   config   bre   false   cond   settings   代码   code   word   

原文地址:http://www.cnblogs.com/mengluo/p/6918218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!