C#实现手机发送验证码

 
目录

具体实现——封装一个类里,U层直接调用

配置文件的内容如下

验证手机号的正则表达式


首先先需要注册第三方网站,给大家推荐一个网站互亿无线,注册了之后每个用户可以免费发送50条短信。

下面以C#实现手机发送验证码为例。基本思路是本地生成一个4位数的随机数,然后以本地的用户名+密码+随机数拼接成一个字符串,转换为二进制数据,以网络流的形式发送到“互亿无线”的网站上,接下来的工作网站就帮你完成了。

具体实现——封装一个类里,U层直接调用

public class Phone
{
     public static string PostUrl = ConfigurationManager.AppSettings["WebReference.Service.PostUrl"];//写在了配置文件中
     /// <summary>
     /// 实现发送验证码
     /// </summary>
     /// <param name="phoneno">手机号</param>
     /// <returns>验证码</returns>
     public static  int PhoneNo(string phoneno)
     {
         string account = "******";//登录“互亿无线网站”查看用户名 登录用户中心->验证码通知短信>产品总览->API接口信息->APIID
         string password = "*******"; //登录“互亿无线网站”查看密码 登录用户中心->验证码通知短信>产品总览->API接口信息->APIKEY
         string mobile = phoneno;
         //string mobile = Request.QueryString["mobile"];
         Random rad = new Random();
         int mobile_code = rad.Next(1000, 10000);   //生成随机数
         //textBox3.Text = mobile_code.ToString();返回值
         string content = "您的验证码是:" + 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)); //将字符串postStrTpl中的格式项替换为四个个指定的 Object 实例的值的文本等效项。再转为二进制数据

         HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);//对统一资源标识符 (URI) 发出请求。 这是一个 abstract 类。
         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);

             //code状态返回值;msg查询结果描述
             string res = reader.ReadToEnd();
             int len1 = res.IndexOf("</code>");
             int len2 = res.IndexOf("<code>");
             string code = res.Substring((len2 + 6), (len1 - len2 - 6));

             int len3 = res.IndexOf("</msg>");
             int len4 = res.IndexOf("<msg>");
             string msg = res.Substring((len4 + 5), (len3 - len4 - 5));
             //MessageBox.Show(msg);
             return mobile_code;
         }
         else
         {
             return 0;
             //访问失败
         }
     }
       
 }

配置文件的内容如下

<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>

验证手机号的正则表达式

/// <summary>
/// 验证手机号的正则表达式
/// </summary>
/// <param name="phoneid">手机号</param>
/// <returns>bool值</returns>
public static bool VailPhoneCode(string phoneid)
{
       string str = @"^1[3-9]\d{9}$";  
       Regex regex = new Regex(str);//正则表达式类
       if (regex.IsMatch(phoneid))//Regex验证
       {
            return true;
       }
       else
       {
            return false;
       }
}

我这里还有其他语言实现手机发送验证码源码,大家可以私信我。私发给你哦。

如果本篇博客对您有一定的帮助,大家记得留言+点赞哦。

评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小张自由—>张有博

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值