C# 互亿无线发送消息
官方demo
我们做登录注册的时候难免要给用户发送验证码,今天来给大家介绍互亿无线的消息发送,大部分代码是参考官方demo的,废话不多说,直接上代码,内联代码片
。
/// <summary>
/// 验证码互亿无线
/// </summary>
public class Captcha
{
public static string PostUrl = ConfigurationManager.AppSettings["WebReference.Service.PostUrl"];
public void Page_Load(object sender, EventArgs e)
{
string account = " ";//查看用户名 登录用户中心->验证码通知短信>产品总览->API接口信息->APIID
string password = " "; //查看密码 登录用户中心->验证码通知短信>产品总览->API接口信息->APIKEY
string mobile =System.Web.HttpContext.Current.Request.QueryString["mobile"];
Random rad = new Random();
int mobile_code = rad.Next(100000, 1000000);
string content = "您的验证码是:" + mobile_code + " 。请不要把验证码泄露给其他人。";
//Session["mobile"] = mobile;
//Session["mobile_code"] = 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));
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);
//Response.Write(reader.ReadToEnd());
string res = reader.ReadToEnd();
int len1 = res.IndexOf("</code>");
int len2 = res.IndexOf("<code>");
string code = res.Substring((len2 + 6), (len1 - len2 - 6));
//Response.Write(code);
int len3 = res.IndexOf("</msg>");
int len4 = res.IndexOf("<msg>");
string msg = res.Substring((len4 + 5), (len3 - len4 - 5));
System.Web.HttpContext.Current.Response.Write(msg);
System.Web.HttpContext.Current.Response.End();
}
else
{
//访问失败
}
}
}
刚刚开始写博客,有问题的话请指点,蟹蟹
下一篇,阿里云的短信发送