1. 程式人生 > >.net 實現之短信驗證碼

.net 實現之短信驗證碼

onsubmit amr div orm ptc 觸發 訂單 void else

接口類型:互億無線觸發短信接口,支持發送驗證碼短信、訂單通知短信等。

賬戶註冊:請通過該地址開通賬戶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 實現之短信驗證碼