首页 > 代码库 > .net 实现之短信验证码

.net 实现之短信验证码

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

账户注册:请通过该地址开通账户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 = http://www.mamicode.com/sTime;>

  

<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=http://www.mamicode.com/" 发送手机验证码 " 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 = http://www.mamicode.com/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://www.mamicode.com/"http://106.ihuyi.cn/webservice/sms.php?method=Submit"/>
    <add key="WebReference.sms" value=http://www.mamicode.com/"http://106.ihuyi.cn/webservice/sms.php?smsService"/>
  </appSettings>

 

     

.net 实现之短信验证码