发送验证码到手机
界面代码
<p><button style="width: 22%;height:40px;line-height:40px;background:#DDDDDD;font-weight: bold;border-radius: 1px;" onclick="get_Verification_Code()" class="verification_text" id="verification_Code">
获取验证码
</button><span id="result" style="color:red"></span> </p>
<p>
<input type="text" id="result_text" placeholder="验证码">
</p>
/js 代码
//获取验证码 11-11
var timerc=60;
function get_Verification_Code() {
$('#verification_Code').attr('disabled', "disabled");
if (timerc > 0) {
if (timerc == 60) {
var phones = document.getElementById('Cus_tel').value.trim();
reg = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
if (!reg.test(phones)) {
alert("输入的联系方式有误,请重新输入!");
return false;
}
else {
ajaxHelper('/ServicerList/GetVerificationCode?phone=' + phones, 'GET', null).done(function (dt) {
verification_code = dt;
if (dt == "发送失败") {
$('#result').text("未发送");
} else {
$('#result').text("已发送");
}
});
}
}
if ($('#result').text() == "未发送") {
return false;
}
timerc--;
$('.verification_text').text(timerc);
setTimeout("get_Verification_Code()", 1000);
} else {
$('.verification_text').text("重新获取");
$('#verification_Code').removeAttr('disabled');
timerc = 60;
}
}
[HttpGet]
public JsonResult GetVerificationCode(string phone)
{
string account = "jiekou-clcs-11";
string password = "Tch655888";
string mobile = phone;
string content = "您好,您的验证码是123456";
string postStrTpl = "account={0}&pswd={1}&mobile={2}&msg={3}&needstatus=true&product=&extno=";
string dt = "";
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);
//dt = reader.ReadToEnd();
//反序列化upfileMmsMsg.Text
//实现自己的逻辑
dt = "123456";
}
else
{
//访问失败
dt = "发送失败";
}
return Json(dt, JsonRequestBehavior.AllowGet);
}
//web.config 文件
<appSettings>
<add key="WebReference.Service.PostUrl" value="http://222.73.117.158/msg/HttpBatchSendSM"/>
<add key="WebReference.Service.BalanceQueryUrl" value="http://222.73.117.158/msg/QueryBalance"/>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>