不多说费话了,直接上代码,有什么不懂的朋友可以给我留言,关于代码中返回值代表多少请参考官网,短信接口:http://www.56dxw.com 关于js的下载可以百度搜索一下,关于图片验证码可以从网上找一下,在这儿只记录一下如何调用短信接口实现短信的发送的
reg.aspx=========================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="reg.aspx.cs" Inherits="SpeakIsDeom.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>
<style>
.spanimg {display:none;}
.spanimgc { display:block;}
</style>
<script type="text/javascript">
/*-------------------------------------------*/
var InterValObj; //timer变量,控制时间
var count = 10; //间隔函数,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
//向后台发送处理数据
$.ajax({
type: "POST", //用POST方式传输
dataType: "text", //数据格式:JSON
url: ‘regmsg.aspx‘, //目标地址
data: "action=isnote&istel=" + ttel + "&code=" + tucode,
error: function (XMLHttpRequest, textStatus, errorThrown) { },
success: function (msg)
{
//1代表是发送短信成功,开始调用计时器
if(msg=="1")
{
InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次
alert("短信已经发送请注意查收");
}
else
{
refCode();
alert(msg);//返回具体参数请参考官网:http://www.56dxw.com
}
}
});
}
//timer处理函数文字短信
function SetRemainTime() {
if (curCount == 0) {
window.clearInterval(InterValObj);//停止计时器
$(".spanimgc").remove();
$("#sendMsg").append(‘<span class="spanimgc spanimg"> <a href="javascript:void(0);" onclick=sendMessageSpeak()>没收到短信?点此获取语音验证码</a></span>‘);
code = ""; //清除验证码。如果不清除,过时间后,输入收到的验证码依然有效
}
else {
curCount--;
$(".spanimg").remove();
$("#sendMsg").append(‘<span class="spanimgc spanimg">如果<font color=red>‘+curCount+‘</font>秒后您没有收到短信,请获取语音验证码</span>‘);
}
}
function refCode() {
$(‘#valiCode‘).hide().attr(‘src‘,
‘ValidateCode.aspx?id=‘ + Math.random()).fadeIn();
// ‘ValidateCode.aspx?id=‘ + Math.floor(Math.random() * 100)).fadeIn();
$("#valiCode").val("");
}
//调用语音短信
function sendMessageSpeak() {
curCount = count;
var dealType; //验证方式
var ttel=$("#t_tel").val();//用户uid
var tucode=$("#txt_code").val();//用户uid
//向后台发送处理数据
$.ajax({
type: "POST", //用POST方式传输
dataType: "text", //数据格式:JSON
url: ‘regmsg.aspx‘, //目标地址
data: "action=speak&istel=" + ttel + "&code=" + tucode,
error: function (XMLHttpRequest, textStatus, errorThrown) { },
success: function (msg)
{
//1代表是发送短信成功,开始调用计时器
if(msg=="1")
{
InterValObj = window.setInterval(SetRemainTimeSpeak, 1000); //启动计时器,1秒执行一次
alert("语音短信已经由031183820961的号码为您发送,请查收");
}
else
{
refCode();
alert(msg);
}
}
});
}
//timer处理函数语音短信
function SetRemainTimeSpeak() {
if (curCount == 0) {
window.clearInterval(InterValObj);//停止计时器
code = ""; //清除验证码。如果不清除,过时间后,输入收到的验证码依然有效
}
else {
curCount--;
$(".spanimgc").remove();
$("#sendMsg").append(‘<span class="spanimgc spanimg">由031183820961的号码为您播报验证码,请注意接收</span>‘);
}
}
</script>
</head>
<body style="text-align:left;">
<form id="form1" runat="server">
说明:<br />
1.以下验证码例子采用的是先获取手机号文字验证码,如果文字验证码收不到,将采用语音播放验证码的形式,两种形式的结合,基本避免个别手机号收不到的问题,从而是验证码的成功率接近100%,有效的避免客户的流失。<br />
2.文字短信验证码和语音短信验证码只是修改一下调用接口的文件名称即可,需要注意的是语音验证码内容是4或6位的数字。<br />
3.以上只是一个简单的调用的例子,关于返回值可参考官网<br />
4.以上先加上图片验证码,然后才可以获取手机验证码,主要是为了防止有些软件恶意获取手机号给它人发送,从而造成短信的浪费,当然方法有很多。不一定非要这一种,这种方法也可能不能完全避免软件恶意获取,但相对还是有效的。
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td align="right" style="width:154px; height: 23px;">56短信网测试</td>
<td style="width: 151px; height: 23px;"> </td>
<td width="921" style="height: 23px"> </td>
</tr>
<tr>
<td style="width:154px; 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;"> 由于每天测试人比较多,我们只允许一个号码一天测试2条</td>
</tr>
<tr>
<td style="width: 154px" 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="#" onclick="javascript:document.getElementById(‘valiCode‘).src=‘ValidateCode.aspx?id=‘+Math.random();return false;">换一张</a></td>
</tr>
<tr>
<td style="width: 154px; 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;" id="sendMsg"><span id="spanimg1" class="spanimgc spanimg"><input id="btnSendCode" type="button" value="获取短信验证码" style="border: 1px solid #999999;height:25px; width: 128px;" /></span></td>
</tr>
<tr>
<td style="width: 154px; 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>
</form>
</body>
</html>
reg.aspx.cs==================================================================
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;
namespace SpeakIsDeom
{
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 && !string.IsNullOrEmpty(goocode))
{
Response.Write("验证码已经正确,您注册成功");
Response.End();
}
else
{
Response.Write("验证码失败,请检查");
}
}
}
}
regmsg.aspx.cs=================================================================
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;
namespace SpeakIsDeom
{
public partial class regmsg : System.Web.UI.Page
{
//官方网站:http://www.56dxw.com
public string _tel, _msg,_action;
protected void Page_Load(object sender, EventArgs e)
{
_tel = Request["istel"];//手机号
_msg = Request["code"];//图片输入的验证码
_action = Request["action"];//isnote 代表文字短信 speak代表语音短信
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(_tel) && !string.IsNullOrEmpty(_msg))
{
string r = SendNoteCode(_tel, _msg);
switch (r)//返回1代表成功
{
case "1":
Response.Write("1");
break;
case "-5":
//if (_action == "isnote")
//{
// Response.Write("nonote");
//}
//else
//{
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)
{
//用户名:speak,密码:a4ffolln,企业ID:2505,平台:10690.
string cid = "100";
string username = "test";
string userpwd = "test";
string s_snumber = Number(6, false);
string _sms = "10690";
//由于语音传过去的只是数字即可,文字验证码可以写上内容
string msg = s_snumber;
string tel = _tel;
string url = "";
//发送普通短信用这个接口
_action = Request["action"];//isnote 代表文字短信 speak代表语音短信
if (_action == "speak")//语音验证码
{
url = "http://jiekou.56dxw.com/sms/SpeakNoteInterface.aspx?comid=" + cid + "&username=" + username + "&userpwd=" + userpwd + "&handtel=" + tel + "&sendcontent=" + msg + "&sendtime=&smsnumber=" + _sms + "";
}
else//文字验证码
{
msg = "您的验证码是:" + s_snumber + "【56短信网】";//【56短信网】 可以换成自己的签名,签名一般用公司或网站的简称
msg = HttpUtility.UrlEncode(msg, System.Text.Encoding.GetEncoding("gbk"));//由于网站是utf-8编码,要进行一下转码,不然会是乱码,调用webs的不用转码
url = "http://jiekou.56dxw.com/sms/HttpInterface.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
}
}
本文出自 “爱好者” 博客,请务必保留此出处http://10864824.blog.51cto.com/10854824/1709957
原文地址:http://10864824.blog.51cto.com/10854824/1709957