Sms.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. namespace Utils
  7. {
  8. /// <summary>
  9. /// 手机短信发送
  10. /// </summary>
  11. public class Sms
  12. {
  13. #region 私有函数
  14. //创建一个随机数实例
  15. private static Random ro = new Random();
  16. private static string getDictionaryData(Dictionary<string, object> data)
  17. {
  18. string ret = null;
  19. foreach (KeyValuePair<string, object> item in data)
  20. {
  21. if (item.Value != null && item.Value.GetType() == typeof(Dictionary<string, object>))
  22. {
  23. ret += item.Key.ToString() + "={";
  24. ret += getDictionaryData((Dictionary<string, object>)item.Value);
  25. ret += "};";
  26. }
  27. else
  28. {
  29. ret += item.Key.ToString() + "=" + (item.Value == null ? "null" : item.Value.ToString()) + ";";
  30. }
  31. }
  32. return ret;
  33. }
  34. #endregion
  35. /// <summary>
  36. /// 发送短信
  37. /// </summary>
  38. /// <param name="token">用户身份标识</param>
  39. /// <param name="sPhone">要发送的手机号-暂不支持“,”分隔发送</param>
  40. /// <param name="sTempIndex">
  41. /// 模板:1-您的验证码是:【变量】。请不要把验证码泄露给其他人。如非本人操作,可不用理会!
  42. /// 模板2-您的注册验证码是:【变量】。请勿泄露他人。如非本人操作,可不用理会!欢迎使用融资通。
  43. /// </param>
  44. /// <param name="sCode">要发送的验证码</param>
  45. /// <returns></returns>
  46. public static string sMsSend(string token, string sPhone, string sTempIndex, string sCode)
  47. {
  48. Encoding encoding = Encoding.GetEncoding("utf-8");
  49. string postData = "token=" + token + "&sSphone=" + sPhone + "&sTempIndex=" + sTempIndex + "&sCode=" + sCode;
  50. byte[] data = encoding.GetBytes(postData);
  51. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://apix.nbbuy.com/sms/sms.ashx");
  52. myRequest.Method = "POST";
  53. myRequest.Timeout = 10000;
  54. myRequest.ContentType = "application/x-www-form-urlencoded";
  55. myRequest.ContentLength = data.Length;
  56. Stream newStream = myRequest.GetRequestStream();
  57. newStream.Write(data, 0, data.Length);
  58. newStream.Close();
  59. HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
  60. StreamReader sreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  61. string read = sreader.ReadToEnd();
  62. return read;
  63. }
  64. /// <summary>
  65. /// 互亿短信
  66. /// </summary>
  67. /// 模板:1-您的验证码是:【变量】。请不要把验证码泄露给其他人。如非本人操作,可不用理会!
  68. /// 模板2-您的注册验证码是:【变量】。请勿泄露他人。如非本人操作,可不用理会!欢迎使用融资通。
  69. /// <param name="mobile"></param>
  70. /// <param name="content"></param>
  71. /// <returns></returns>
  72. public static string SMSSendByhuyi(string mobile, string content)
  73. {
  74. string account = "cf_380568887";
  75. string password = StringHelper.EncryptMD5("888888889");//密码可以使用明文密码或使用32位MD5加密
  76. string postUrl = "http://106.ihuyi.cn/webservice/sms.php?method=Submit"; //短信接口地址
  77. string postStrTpl = "account={0}&password={1}&mobile={2}&content={3}";
  78. UTF8Encoding encoding = new UTF8Encoding();
  79. byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content));
  80. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(postUrl);
  81. myRequest.Method = "POST";
  82. myRequest.ContentType = "application/x-www-form-urlencoded";
  83. myRequest.ContentLength = postData.Length;
  84. Stream newStream = myRequest.GetRequestStream();
  85. // Send the data.
  86. newStream.Write(postData, 0, postData.Length);
  87. newStream.Flush();
  88. newStream.Close();
  89. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  90. if (myResponse.StatusCode == HttpStatusCode.OK)
  91. {
  92. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  93. //Response.Write(reader.ReadToEnd());
  94. string res = reader.ReadToEnd();
  95. int len1 = res.IndexOf("</code>");
  96. int len2 = res.IndexOf("<code>");
  97. string code = res.Substring((len2 + 6), (len1 - len2 - 6));
  98. //Response.Write(code);
  99. int len3 = res.IndexOf("</msg>");
  100. int len4 = res.IndexOf("<msg>");
  101. string msg = res.Substring((len4 + 5), (len3 - len4 - 5));
  102. return msg;
  103. }
  104. else
  105. {
  106. return "发送失败";
  107. //访问失败
  108. }
  109. }
  110. /// <summary>
  111. /// 获取6位短信验证码
  112. /// </summary>
  113. /// <returns></returns>
  114. public static string GetSMSCode()
  115. {
  116. //获取一个6位的随机数
  117. string rnd = ro.Next(100000, 999999).ToString();
  118. //EventLog.WriteLog(rnd);
  119. //赋值session
  120. //HttpContext.Current.Session["SMSCode"] = rnd;
  121. return rnd;
  122. }
  123. }
  124. }