SendSMS.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Security;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Text;
  9. using Library;
  10. // using MongoDB.Bson;
  11. // using MongoDB.Driver;
  12. // using MongoDB.Driver.Linq;
  13. namespace MySystem
  14. {
  15. public class SendSMS
  16. {
  17. public readonly static SendSMS Instance = new SendSMS();
  18. private SendSMS()
  19. {
  20. }
  21. #region 发送短信验证码
  22. private string host = "https://gyytz.market.alicloudapi.com";
  23. private string path = "/sms/smsSend";
  24. private string method = "POST";
  25. private string appcode = "8e5704921ca3422f80f0deb935a7ddc6";
  26. public string Do(string mobile, string code)
  27. {
  28. string logString = "";
  29. String querys = "mobile=" + mobile + "&param=**code**%3A" + code + "%2C**minute**%3A5&smsSignId=8b278cf2bb2a4b3c818cc7a436307a12&templateId=908e94ccf08b4476ba6c876d13f084ad";
  30. logString += "\n" + querys;
  31. String bodys = "";
  32. String url = host + path;
  33. HttpWebRequest httpRequest = null;
  34. HttpWebResponse httpResponse = null;
  35. if (0 < querys.Length)
  36. {
  37. url = url + "?" + querys;
  38. }
  39. if (host.Contains("https://"))
  40. {
  41. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  42. httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
  43. }
  44. else
  45. {
  46. httpRequest = (HttpWebRequest)WebRequest.Create(url);
  47. }
  48. httpRequest.Method = method;
  49. httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
  50. if (0 < bodys.Length)
  51. {
  52. byte[] data = Encoding.UTF8.GetBytes(bodys);
  53. using (Stream stream = httpRequest.GetRequestStream())
  54. {
  55. stream.Write(data, 0, data.Length);
  56. }
  57. }
  58. try
  59. {
  60. httpResponse = (HttpWebResponse)httpRequest.GetResponse();
  61. }
  62. catch (WebException ex)
  63. {
  64. httpResponse = (HttpWebResponse)ex.Response;
  65. }
  66. Stream st = httpResponse.GetResponseStream();
  67. StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
  68. string result = reader.ReadToEnd();
  69. logString += "\n" + result;
  70. LogHelper.Instance.WriteLog(logString, "阿里云短信验证码日志", true);
  71. return result;
  72. }
  73. public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  74. {
  75. return true;
  76. }
  77. #endregion
  78. }
  79. }