短信接口源码(实现按钮倒计时的功能)

不多说费话了,直接上代码,有什么不懂的朋友可以给我留言,关于代码中返回值代表多少请参考官网,短信接口:http://www.56dxw.com,关于js的下载可以百度搜索一下

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _reg : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string scode = txt_iscode.Text.ToString().Trim();
        string goocode = Convert.ToString(Session["telcodemsg"]);
        if (goocode == scode)
        {

            Response.Write("验证码已经正确,您注册成功");
            Response.End();
        }
        else
        {
            Response.Write("验证码失败,请检查");

        }
    }
}


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="reg.aspx.cs" Inherits="_reg" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    

    <script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        /*-------------------------------------------*/
        var InterValObj; //timer变量,控制时间
var count = 120; //间隔函数,1秒执行
var curCount;//当前剩余秒数
var code = ""; //验证码
var codeLength = 6;//验证码长度
function sendMessage() {
            curCount = count;
            var dealType; //验证方式
var ttel=$("#t_tel").val();//用户uid
var  tucode=$("#txt_code").val();//用户uid
//alert(uid);
//if ($("#phone").attr("checked") == true) {
//                dealType = "phone";
//            }
//            else {
//                dealType = "email";
//            }
            //产生验证码
//for (var i = 0; i < codeLength; i++) {
//                code += parseInt(Math.random() * 9).toString();
//            }
          //向后台发送处理数据
                $.ajax({
                    type: "POST", //用POST方式传输
                    dataType: "text", //数据格式:JSON
                    url: 'regmsg.aspx', //目标地址
                    data: "istel=" + ttel + "&code=" + tucode,
                    error: function (XMLHttpRequest, textStatus, errorThrown) { },
                    success: function (msg)
                    {
                        //1代表是发送短信成功,开始调用计时器
                        if(msg=="1")
                        {
                          //设置button效果,开始计时
                            $("#btnSendCode").attr("disabled", "true");
                            $("#btnSendCode").val(curCount + "秒后重新获取");
                            InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次
                              alert("语音短信已经由031183820961的号码为您发送,请查收");
                        }
                        else
                        {
                            refCode();
                            alert(msg);
                        }
                    }
                });
            }
        //timer处理函数
function SetRemainTime() {
            if (curCount == 0) {                
                window.clearInterval(InterValObj);//停止计时器
                $("#btnSendCode").removeAttr("disabled");//启用按钮
                $("#btnSendCode").val("重新发送验证码");
                code = ""; //清除验证码。如果不清除,过时间后,输入收到的验证码依然有效    
            }
            else {
                curCount--;
                $("#btnSendCode").val(curCount + "秒后重新获取");
            }
        }
        
        
        
        function refCode() {
	$('#valiCode').hide().attr('src',
	    'ValidateCode.aspx?id=' + Math.random()).fadeIn();
//		'ValidateCode.aspx?id=' + Math.floor(Math.random() * 100)).fadeIn();
	$("#valiCode").val("");
}
        
        
        
    </script>
</head>
<body>

     <form id="form1" runat="server">
<table width="100%" border="0" cellspacing="2" cellpadding="0">
  <tr>
    <td height="23" align="right" style="width:20%">56短信网测试</td>
    <td height="23" style="width: 151px"> </td>
    <td width="921" height="23"> </td>
  </tr>
  <tr>
    <td style="width:width:20%; height: 26px;" align="right">
        验证手机号:</td>
    <td valign="top" style="width: 151px; height: 26px;"><input id="t_tel" type="text" style="border: 1px solid #999999;height:20px;" /></td>
    <td style="width: 338px; height: 26px;"> </td>
  </tr>
    <tr>
    <td style="width: width:20%" align="right"> 输入图形验证码</td>
    <td style="width: 151px; border-bottom:0px;"> 
        <input id="txt_code" type="text" style="border: 1px solid #999999;height:20px;" runat="server" /></td>
    <td style="width: 338px; border-bottom:0px;" align="left"> <img src="ValidateCode.aspx" alt="验证码" width="490" id="valiCode" style="width: 80px; height: 28px" />看不清,<a title="刷新验证码" href="#" οnclick="javascript:document.getElementById('valiCode').src='ValidateCode.aspx?id='+Math.random();return false;">换一张</a></td>
  </tr>
  <tr>
    <td style="width: width:20%; height: 20px;" align="right">  短信验证码::</td>
    <td align="left" style="width: 151px; height: 20px;">
        <asp:TextBox ID="txt_iscode" runat="server"></asp:TextBox></td>
    <td align="left" style="width: 338px; height: 20px;"><input id="btnSendCode" type="button" value="获取短信验证码" οnclick="sendMessage()"  style="border: 1px solid #999999;height:25px; width: 128px;"  /></td>
  </tr>

  <tr>
    <td style="width: 331px; height: 25px;"> </td>
    <td style="width: 151px; height: 25px;"> <asp:Button ID="Button1" runat="server" Text="注册" Width="114px"  style="border: 1px solid #999999;height:25px; width: 128px;" OnClick="Button1_Click"   /></td>
    <td style="width: 338px; height: 25px;"> </td>
  </tr>
</table>
         <br />
         <br />
         <br />
         <br />
         <br />
         
         
         
         
                 说明:<br />
         1.文字短信验证码和语音短信验证码只是修改一下调用接口的文件名称即可,需要注意的是语音验证码内容是4或6位的数字。<br />
         2.以上只是一个简单的调用的例子,关于返回值可参考官网:http://www.56dxw.com<br />
         3.以上先加上图片验证码,然后才可以获取手机验证码,主要是为了防止有些软件恶意获取手机号给它人发送,从而造成短信的浪费,当然方法有很多。不一定非要这一种,这种方法也可能不能完全避免软件恶意获取,但相对还是有效的。
         <br />




       </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
public partial class regmsg : System.Web.UI.Page
{


    public string _tel, _msg;
    protected void Page_Load(object sender, EventArgs e)
    {
        _tel = Request["istel"];//手机号
        _msg = Request["code"];//图片输入的验证码
        if (!IsPostBack)
        {
            
               if (!string.IsNullOrEmpty(_tel) && !string.IsNullOrEmpty(_msg))
            {
                string r = SendNoteCode(_tel, _msg);
               短信接口:http://www.56dxw.com 返回值需要参考的网站
                switch (r)//返回1代表成功
                {

                    case "1":
                        //Response.Write("语音短信已经由031183820961的号码为您发送,请查收");
                        Response.Write("1");
                        break;

                    case "-5":
                        Response.Write("短信余额不足");
                        break;
                    case "no1":
                        Response.Write("您输入图片中的验证码不正确,无法获取短信验证码,请重新输入");

                        break;
                    case "notel":
                        Response.Write("请检查您的手机号是否正确");

                        break;
                    default:
                        Response.Write("具体的返回值是:" + r + "'");
                        break;


                }
            }
            else
            {
                Response.Write("手机机或验证码不能为空");
            }
        }


    }


    public string SendNoteCode(string _tel, string _tucode)
    {
        //官方网站:http://www.56dxw.com 
        //  string scode = Request["txt_code"].ToString().Trim();
        string scode = _tucode;
        string r = "no1";
        if (IsTel(_tel) == true)
        {

            string twcode = Session["GoogleCode"].ToString();


            if (twcode == scode)
            {
                string cid = "企业ID";
                string username = "用户名";
                string userpwd = "密码";
                string s_snumber = Number(6, false);
                string _sms = "10690";
                //由于语音传过去的只是数字即可,文字验证码可以写上内容
                // string msg = "您的验证码是:" + s_snumber + "【56短信网】";//【56短信网】 可以换成自己的签名,签名一般用公司或网站的简称
                string msg = s_snumber;
                msg = HttpUtility.UrlEncode(msg, System.Text.Encoding.GetEncoding("gbk"));//由于网站是utf-8编码,要进行一下转码,不然会是乱码,调用webs的不用转码
                string tel = _tel;

                //发送普通短信用这个接口
                // string url = "http://jiekou.56dxw.com/sms/HttpInterface.aspx?comid=" + cid + "&username=" + username + "&userpwd=" + userpwd + "&handtel=" + tel + "&sendcontent=" + msg + "&sendtime=&smsnumber=" + _sms + "";
              
                //发送语音短信用这个接口,只是修改一下文件名称,用户名和密码,企业ID都不用修改的,还是很方便的
                string url = "http://jiekou.56dxw.com/sms/SpeakNoteInterface.aspx?comid=" + cid + "&username=" + username + "&userpwd=" + userpwd + "&handtel=" + tel + "&sendcontent=" + msg + "&sendtime=&smsnumber=" + _sms + "";

                r = GetSend(url, "gbk");
                if (r == "1")
                {
                    Session["telcodemsg"] = s_snumber;//为了方便起间,斩时用session保存值
                }

            }
            else
            {
                r = "no1";
            }
        }
        else
        {
            r = "notel";
        }
        return r;
    }


    #region 生成随机数

    /// <summary>
    /// 生成随机数字
    /// </summary>
    /// <param name="length">生成长度</param>
    /// <returns></returns>
    public static string Number(int Length)
    {
        return Number(Length, false);
    }

    /// <summary>
    /// 生成随机数字
    /// </summary>
    /// <param name="Length">生成长度</param>
    /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
    /// <returns></returns>
    public static string Number(int Length, bool Sleep)
    {
        if (Sleep)
            System.Threading.Thread.Sleep(3);
        string result = "";
        System.Random random = new Random();
        for (int i = 0; i < Length; i++)
        {
            result += random.Next(10).ToString();
        }
        return result;
    }

    /// <summary>
    /// 生成随机字母与数字
    /// </summary>
    /// <param name="IntStr">生成长度</param>
    /// <returns></returns>
    public static string Str(int Length)
    {
        return Str(Length, false);
    }
    /// <summary>
    /// 生成随机字母与数字
    /// </summary>
    /// <param name="Length">生成长度</param>
    /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
    /// <returns></returns>
    public static string Str(int Length, bool Sleep)
    {
        if (Sleep)
            System.Threading.Thread.Sleep(3);
        char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        string result = "";
        int n = Pattern.Length;
        System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
        for (int i = 0; i < Length; i++)
        {
            int rnd = random.Next(0, n);
            result += Pattern[rnd];
        }
        return result;
    }


    /// <summary>
    /// 验证手机号
    /// </summary>
    /// <param name="tel"></param>
    /// <returns></returns>
    public static bool IsTel(string tel)
    {

        if (string.IsNullOrEmpty(tel))
        {
            return false;
        }
        else
        {
            string pattern = "^1[3|4|5|7|8]\\d{9}$";
            if (Regex.Match(tel, pattern, RegexOptions.Compiled).Success)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }



    /// <summary>
    /// 生成随机纯字母随机数
    /// </summary>
    /// <param name="IntStr">生成长度</param>
    /// <returns></returns>
    public static string Str_char(int Length)
    {
        return Str_char(Length, false);
    }

    /// <summary>
    /// 生成随机纯字母随机数
    /// </summary>
    /// <param name="Length">生成长度</param>
    /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
    /// <returns></returns>
    public static string Str_char(int Length, bool Sleep)
    {
        if (Sleep)
            System.Threading.Thread.Sleep(3);
        char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        string result = "";
        int n = Pattern.Length;
        System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
        for (int i = 0; i < Length; i++)
        {
            int rnd = random.Next(0, n);
            result += Pattern[rnd];
        }
        return result;
    }


    /// <summary>
    /// 生成随机纯字母随机数
    /// </summary>
    /// <param name="Length">生成长度</param>
    /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
    /// <returns></returns>
    public static string Str_charSamll(int Length, bool Sleep)
    {
        if (Sleep)
            System.Threading.Thread.Sleep(3);
        char[] Pattern = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        string result = "";
        int n = Pattern.Length;
        System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
        for (int i = 0; i < Length; i++)
        {
            int rnd = random.Next(0, n);
            result += Pattern[rnd];
        }
        return result;
    }

    #endregion


    #region  get方式提交
    public string GetSend(string url, string bm)
    {

        //string url = _url + "?" + postdate;
        WebRequest wRequest = WebRequest.Create(url);
        WebResponse wResponse = wRequest.GetResponse();
        Stream stream = wResponse.GetResponseStream();
        wRequest.Timeout = 5000;
        wRequest.Method = "GET";
        wRequest.ContentType = "application/x-www-form-urlencoded";
        StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
        string r = reader.ReadToEnd();
        wResponse.Close();
        reader.Dispose();
        reader.Close();

        return r;
    }

    #endregion
















}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值