123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text;
- namespace Utils
- {
- /// <summary>
- /// 手机短信发送
- /// </summary>
- public class Sms
- {
- #region 私有函数
- //创建一个随机数实例
- private static Random ro = new Random();
- private static string getDictionaryData(Dictionary<string, object> data)
- {
- string ret = null;
- foreach (KeyValuePair<string, object> item in data)
- {
- if (item.Value != null && item.Value.GetType() == typeof(Dictionary<string, object>))
- {
- ret += item.Key.ToString() + "={";
- ret += getDictionaryData((Dictionary<string, object>)item.Value);
- ret += "};";
- }
- else
- {
- ret += item.Key.ToString() + "=" + (item.Value == null ? "null" : item.Value.ToString()) + ";";
- }
- }
- return ret;
- }
- #endregion
- /// <summary>
- /// 发送短信
- /// </summary>
- /// <param name="token">用户身份标识</param>
- /// <param name="sPhone">要发送的手机号-暂不支持“,”分隔发送</param>
- /// <param name="sTempIndex">
- /// 模板:1-您的验证码是:【变量】。请不要把验证码泄露给其他人。如非本人操作,可不用理会!
- /// 模板2-您的注册验证码是:【变量】。请勿泄露他人。如非本人操作,可不用理会!欢迎使用融资通。
- /// </param>
- /// <param name="sCode">要发送的验证码</param>
- /// <returns></returns>
- public static string sMsSend(string token, string sPhone, string sTempIndex, string sCode)
- {
- Encoding encoding = Encoding.GetEncoding("utf-8");
- string postData = "token=" + token + "&sSphone=" + sPhone + "&sTempIndex=" + sTempIndex + "&sCode=" + sCode;
- byte[] data = encoding.GetBytes(postData);
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://apix.nbbuy.com/sms/sms.ashx");
- myRequest.Method = "POST";
- myRequest.Timeout = 10000;
- myRequest.ContentType = "application/x-www-form-urlencoded";
- myRequest.ContentLength = data.Length;
- Stream newStream = myRequest.GetRequestStream();
- newStream.Write(data, 0, data.Length);
- newStream.Close();
- HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
- StreamReader sreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- string read = sreader.ReadToEnd();
- return read;
- }
- /// <summary>
- /// 互亿短信
- /// </summary>
- /// 模板:1-您的验证码是:【变量】。请不要把验证码泄露给其他人。如非本人操作,可不用理会!
- /// 模板2-您的注册验证码是:【变量】。请勿泄露他人。如非本人操作,可不用理会!欢迎使用融资通。
- /// <param name="mobile"></param>
- /// <param name="content"></param>
- /// <returns></returns>
- public static string SMSSendByhuyi(string mobile, string content)
- {
- string account = "cf_380568887";
- string password = StringHelper.EncryptMD5("888888889");//密码可以使用明文密码或使用32位MD5加密
- string postUrl = "http://106.ihuyi.cn/webservice/sms.php?method=Submit"; //短信接口地址
- 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));
- return msg;
- }
- else
- {
- return "发送失败";
- //访问失败
- }
- }
-
- /// <summary>
- /// 获取6位短信验证码
- /// </summary>
- /// <returns></returns>
- public static string GetSMSCode()
- {
- //获取一个6位的随机数
- string rnd = ro.Next(100000, 999999).ToString();
- //EventLog.WriteLog(rnd);
- //赋值session
- //HttpContext.Current.Session["SMSCode"] = rnd;
- return rnd;
- }
- }
- }
|