赞
踩
当点击button1后向textbox1中输入的号码的手机发送一条验证码短信

网站:添加链接描述

一个label一个textbox一个button

添加一个按钮点击事件
private void button1_Click(object sender, EventArgs e)
{
if (!CallPhone(textBox1.Text.ToString()))
{
label1.Text = "不成功";
}
}
新建一个发送短信的函数叫CallPhone()
private bool CallPhone(string Recipient_Mobile_Num) { // string PostUrl = "http://106.ihuyi.com/webservice/sms.php?method=Submit"; //登录“互亿无线网站”查看用户名 登录用户中心->验证码通知短信>产品总览->API接口信息->APIID string account = "你的APIID"; //登录“互亿无线网站”查看密码 登录用户中心->验证码通知短信>产品总览->API接口信息->APIKEY string password = "你的APIKEY"; //接收短信的用户的手机号码 string mobile = Recipient_Mobile_Num; //随机生成四位数 可以模仿向用户发送验证码 Random rad = new Random(); int mobile_code = rad.Next(1000, 10000); //生成随机数 string content = "您的验证码是:" + mobile_code + " 。请不要把验证码泄露给其他人。"; string postStrTpl = "account={0}&password={1}&mobile={2}&content={3}"; //用户名+密码+注册的手机号+验证码 UTF8Encoding encoding = new UTF8Encoding(); //万国码 //将 account, password, mobile, content 这四个内容添加到postStrTpl字符串当中 //并利用encoding.GetBytes()将括号里面的字符串转化为二进制类型 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(); //间postData合并到 PostUrl中去 newStream.Write(postData, 0, postData.Length); newStream.Flush(); newStream.Close(); //以http://106.ihuyi.com/webservice/sms.php?method=Submit&account=你的APIID&password=你的APIKEY&mobile=接收短信的用户的手机号码&content=您的验证码是:" + mobile_code + " 。请不要把验证码泄露给其他人。" 发起https请求 并获取请求结果 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); if (myResponse.StatusCode == HttpStatusCode.OK) { return true; } else { return false; //访问失败 } }
注意需要修改的地方
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Net; namespace Sing_In { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (!CallPhone(textBox1.Text.ToString())) { label1.Text = "不成功"; } } private bool CallPhone(string Recipient_Mobile_Num) { string PostUrl = "http://106.ihuyi.com/webservice/sms.php?method=Submit"; //登录“互亿无线网站”查看用户名 登录用户中心->验证码通知短信>产品总览->API接口信息->APIID string account = "你的APIID"; //登录“互亿无线网站”查看密码 登录用户中心->验证码通知短信>产品总览->API接口信息->APIKEY string password = "你的APIKEY"; //接收短信的用户的手机号码 string mobile = Recipient_Mobile_Num; //随机生成四位数 可以模仿向用户发送验证码 Random rad = new Random(); int mobile_code = rad.Next(1000, 10000); //生成随机数 string content = "您的验证码是:" + mobile_code + " 。请不要把验证码泄露给其他人。"; string postStrTpl = "account={0}&password={1}&mobile={2}&content={3}"; //用户名+密码+注册的手机号+验证码 UTF8Encoding encoding = new UTF8Encoding(); //万国码 //将 account, password, mobile, content 这四个内容添加到postStrTpl字符串当中 //并利用encoding.GetBytes()将括号里面的字符串转化为二进制类型 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(); //间postData合并到 PostUrl中去 newStream.Write(postData, 0, postData.Length); newStream.Flush(); newStream.Close(); //以http://106.ihuyi.com/webservice/sms.php?method=Submit&account=你的APIID&password=你的APIKEY&mobile=接收短信的用户的手机号码&content=您的验证码是:" + mobile_code + " 。请不要把验证码泄露给其他人。" 发起https请求 并获取请求结果 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); if (myResponse.StatusCode == HttpStatusCode.OK) { return true; } else { return false; //访问失败 } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。