SendSMS.cs 2.7 KB

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