• C# DEMO

    C# DEMO

    功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

    1. using System;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using System.Linq;
    5. using System.Net;
    6. using System.Text;
    7. using System.Threading.Tasks;
    8. using System.Web;
    9. namespace yunpianSmsClient
    10. {
    11. class Program
    12. {
    13. static void Main(string[] args)
    14. {
    15. // 设置为您的apikey(https://www.yunpian.com)可查
    16. string apikey = "xxxxxxxxxxxxxxxxxxxxxxx";
    17. // 发送的手机号
    18. string mobile = "xxxxxxxxxxxxxxxxxxxxxxx";
    19. // 发送模板编号
    20. int tpl_id = 1;
    21. // 发送模板内容
    22. mobile = HttpUtility.UrlEncode(mobile, Encoding.UTF8);
    23. string tpl_value = HttpUtility.UrlEncode(
    24. HttpUtility.UrlEncode("#code#", Encoding.UTF8) + "=" +
    25. HttpUtility.UrlEncode("1234", Encoding.UTF8) + "&" +
    26. HttpUtility.UrlEncode("#company#", Encoding.UTF8) + "=" +
    27. HttpUtility.UrlEncode("云片网", Encoding.UTF8), Encoding.UTF8);
    28. // 发送内容
    29. string text = "【云片网】您的验证码是1234";
    30. // 获取user信息url
    31. string url_get_user = "https://sms.yunpian.com/v2/user/get.json";
    32. // 智能模板发送短信url
    33. string url_send_sms = "https://sms.yunpian.com/v2/sms/single_send.json";
    34. // 指定模板发送短信url
    35. string url_tpl_sms =
    36. "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
    37. // 发送语音短信url
    38. string url_send_voice = "https://voice.yunpian.com/v2/voice/send.json";
    39. string data_get_user = "apikey=" + apikey;
    40. string data_send_sms = "apikey=" + apikey + "&mobile=" + mobile + "&text=" +
    41. text;
    42. string data_tpl_sms = "apikey=" + apikey + "&mobile=" + mobile +
    43. "&tpl_id=" + tpl_id.ToString() + "&tpl_value=" + tpl_value;
    44. string data_send_voice = "apikey=" + apikey + "&mobile=" + mobile +
    45. "&code=" + "1234";
    46. HttpPost(url_get_user, data_get_user);
    47. HttpPost(url_send_sms, data_send_sms);
    48. HttpPost(url_tpl_sms, data_tpl_sms);
    49. HttpPost(url_send_voice, data_send_voice);
    50. }
    51. public static void HttpPost(string Url, string postDataStr)
    52. {
    53. byte[] dataArray = Encoding.UTF8.GetBytes(postDataStr);
    54. // Console.Write(Encoding.UTF8.GetString(dataArray));
    55. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
    56. request.Method = "POST";
    57. request.ContentType = "application/x-www-form-urlencoded";
    58. request.ContentLength = dataArray.Length;
    59. //request.CookieContainer = cookie;
    60. Stream dataStream = request.GetRequestStream();
    61. dataStream.Write(dataArray, 0, dataArray.Length);
    62. dataStream.Close();
    63. try
    64. {
    65. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    66. StreamReader reader =
    67. new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    68. String res = reader.ReadToEnd();
    69. reader.Close();
    70. Console.Write("\nResponse Content:\n" + res + "\n");
    71. }
    72. catch(WebException e)
    73. {
    74. Console.Write(e.Message + e.ToString());
    75. Stream stream = e.Response.GetResponseStream();
    76. StreamReader reader =
    77. new StreamReader(stream, Encoding.UTF8);
    78. String res = reader.ReadToEnd();
    79. reader.Close();
    80. Console.Write("\nResponse Content:\n" + res + "\n");
    81. }
    82. }
    83. }
    84. }