SendSMS.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 querys = "mobile=" + mobile + "&param=**code**%3A" + code + "%2C**minute**%3A5&smsSignId=8b278cf2bb2a4b3c818cc7a436307a12&templateId=908e94ccf08b4476ba6c876d13f084ad";
  29. String bodys = "";
  30. String url = host + path;
  31. HttpWebRequest httpRequest = null;
  32. HttpWebResponse httpResponse = null;
  33. if (0 < querys.Length)
  34. {
  35. url = url + "?" + querys;
  36. }
  37. if (host.Contains("https://"))
  38. {
  39. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  40. httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
  41. }
  42. else
  43. {
  44. httpRequest = (HttpWebRequest)WebRequest.Create(url);
  45. }
  46. httpRequest.Method = method;
  47. httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
  48. if (0 < bodys.Length)
  49. {
  50. byte[] data = Encoding.UTF8.GetBytes(bodys);
  51. using (Stream stream = httpRequest.GetRequestStream())
  52. {
  53. stream.Write(data, 0, data.Length);
  54. }
  55. }
  56. try
  57. {
  58. httpResponse = (HttpWebResponse)httpRequest.GetResponse();
  59. }
  60. catch (WebException ex)
  61. {
  62. httpResponse = (HttpWebResponse)ex.Response;
  63. }
  64. Stream st = httpResponse.GetResponseStream();
  65. StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
  66. string result = reader.ReadToEnd();
  67. return result;
  68. }
  69. public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  70. {
  71. return true;
  72. }
  73. #endregion
  74. }
  75. }