目次
特定の実装-クラスをカプセル化し、Uレイヤーは直接呼び出します
最初にサードパーティのサイトを登録する必要があります。各ユーザーが登録した後、50SMSを無料で送信できる相互に100万ワイヤレスのサイトをお勧めします。
例として、検証コードを送信する携帯電話のC#実装を取り上げましょう。基本的な考え方は、4桁の乱数をローカルで生成し、それをローカルユーザー名+パスワード+乱数の文字列につなぎ合わせ、バイナリデータに変換して、の「MuyiWireless」のWebサイトに送信することです。ネットワークストリームの形式。、次の仕事のウェブサイトがあなたのためにそれを行います。
特定の実装-クラスをカプセル化し、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;
}
}
携帯電話から送信される確認コードのソースコードを実装するために、他の言語がここにあります。プライベートメッセージを送信できます。個人的に送ってください。
このブログがお役に立てば、メッセージを残してください+いいね!