C# realize mobile phone send verification code

 
table of Contents

Let's take the C# implementation of mobile phone sending verification code as an example. The basic idea is to generate a 4-digit random number locally, then splice it into a string with the local user name + password + random number, convert it into binary data, and send it to the website of "Muyi Wireless" in the form of a network stream , The next job website will do it for you.

Specific implementation-encapsulate a class, U layer directly call

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;
             //访问失败
         }
     }
       
 }

The content of the configuration file is as follows

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

Regular expression for verifying mobile phone number

/// <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;
       }
}

I have other languages ​​here to implement the source code of the verification code sent by the mobile phone. You can send me private messages. Send it to you privately.

If this blog is helpful to you, please remember to leave a message + like it.

Guess you like

Origin blog.csdn.net/promsing/article/details/109561046