全部产品
Search
文档中心

C#

更新时间: 2019-11-26

入参列表

参数名称 参数类型 必填与否 样例取值 参数说明
MessageType String 必填 SmsReport 有一种消息类型: SmsReport。
QueueName String 必填 Alicom-Queue-xxxxxx-SmsReport 阿里巴巴提供的消息队列名称,在短信控制台——系统设置可以查看到。

出参列表

出参名称 出参类型 样例取值 参数说明
MessageId String 10080303003003 短信的发送ID
To String 452220121 短信的发送号码
Status Number 3 短信的发送状态: 1 (Submitted), 2 (Failed), and 3 (Delivered).
ErrorCode String success 短信发送返回的错误码
ErrorDescription String success 短信发送返回的错误码详细描述
SendDate String Mon, 24 Dec 2018 16:58:22 +0800 短信转发给运营商的时间
ReceiveDate String Mon, 24 Dec 2018 16:58:22 +0800 短信收到运营商回执的时间
NumberDetail String 号码的详细属性
└Country String Hongkong, China 号码所属国家
└Region String HongKong Number Region
└Carrier String CMI 号码所属的运营商网络

技术对接步骤

1.SDK下载地址:点击查看下载地址

2.编写样例程序

调用接口时,请注意:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aliyun.Acs.Core;
  6. using Aliyun.Acs.Core.Exceptions;
  7. using Aliyun.Acs.Core.Profile;
  8. using Aliyun.Acs.Dybaseapi.Model.V20180501;
  9. using Aliyun.MNS;
  10. using Aliyun.MNS.Model;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace ConsoleApplication2
  14. {
  15. class Program
  16. {
  17. //Set the size of asynchronous thread
  18. private static int maxThread = 2;
  19. static String accessId = "yourAccessId";
  20. static String accessSecret = "yourAccessSecret";
  21. //Message Type, there is only two type: smsreport, upmessage
  22. static String messageType = "SmsReport";
  23. //Message Quenue Name, you can get it from SMS console, like:Alicom-Queue-xxxxxx-SmsReport
  24. static String queueName = "Alicom-Queue-xxxxxxxxxxxx-SmsReport";
  25. static String domainForPop = "sms-intl.ap-southeast-1.aliyuncs.com";
  26. static String regionIdForPop = "ap-southeast-1";
  27. static String productName = "Dybaseapi";
  28. static IAcsClient acsClient = null;
  29. public static IAcsClient InitAcsClient(String regionIdForPop, String accessId, String accessSecret, String productName, String domainForPop)
  30. {
  31. IClientProfile profile = DefaultProfile.GetProfile(regionIdForPop, accessId, accessSecret);
  32. DefaultProfile.AddEndpoint(regionIdForPop, regionIdForPop, productName, domainForPop);
  33. IAcsClient acsClient = new DefaultAcsClient(profile);
  34. return acsClient;
  35. }
  36. private static void InitData()
  37. {
  38. acsClient = InitAcsClient(regionIdForPop, accessId, accessSecret, productName, domainForPop);
  39. }
  40. static void Main(string[] args)
  41. {
  42. InitData();
  43. for (int i = 0; i < maxThread; i++)
  44. {
  45. TestTask testTask = new TestTask("PullMessageTask-thread-" + i, messageType, queueName, acsClient);
  46. Thread t = new Thread(new ThreadStart(testTask.Handle));
  47. t.Start();
  48. }
  49. Console.ReadKey();
  50. }
  51. }
  52. class TestTask
  53. {
  54. private object o = new object();
  55. private int sleepTime = 50;
  56. public String name { get; private set; }
  57. public String messageType { get; private set; }
  58. public String queueName { get; private set; }
  59. public int TaskID { get; private set; }
  60. public IAcsClient acsClient { get; private set; }
  61. public TestTask(String name, String messageType, String queueName, IAcsClient acsClient)
  62. {
  63. this.name = name;
  64. this.messageType = messageType;
  65. this.queueName = queueName;
  66. this.acsClient = acsClient;
  67. }
  68. long bufferTime = 60 * 2;
  69. String mnsAccountEndpoint = "https://1493622401794734.mns.ap-southeast-1.aliyuncs.com/";
  70. Dictionary<string, QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO> tokenMap = new Dictionary<string, QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO>();
  71. Dictionary<string, Queue> queueMap = new Dictionary<string, Queue>();
  72. public QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO GetTokenByMessageType(IAcsClient acsClient, String messageType)
  73. {
  74. QueryTokenForMnsQueueRequest request = new QueryTokenForMnsQueueRequest();
  75. request.MessageType = messageType;
  76. QueryTokenForMnsQueueResponse queryTokenForMnsQueueResponse = acsClient.GetAcsResponse(request);
  77. QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO token = queryTokenForMnsQueueResponse.MessageTokenDTO;
  78. return token;
  79. }
  80. public void Handle()
  81. {
  82. while (true)
  83. {
  84. try
  85. {
  86. QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO token = null;
  87. Queue queue = null;
  88. lock (o)
  89. {
  90. if (tokenMap.ContainsKey(messageType))
  91. {
  92. token = tokenMap[messageType];
  93. }
  94. if (queueMap.ContainsKey(queueName))
  95. {
  96. queue = queueMap[queueName];
  97. }
  98. TimeSpan ts = new TimeSpan(0);
  99. if (token != null)
  100. {
  101. DateTime b = Convert.ToDateTime(token.ExpireTime);
  102. DateTime c = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"));
  103. ts = b - c;
  104. }
  105. if (token == null || ts.TotalSeconds < bufferTime || queue == null)
  106. {
  107. token = GetTokenByMessageType(acsClient, messageType);
  108. IMNS client = new Aliyun.MNS.MNSClient(token.AccessKeyId, token.AccessKeySecret, mnsAccountEndpoint, token.SecurityToken);
  109. queue = client.GetNativeQueue(queueName);
  110. if (tokenMap.ContainsKey(messageType))
  111. {
  112. tokenMap.Remove(messageType);
  113. }
  114. if (queueMap.ContainsKey(queueName))
  115. {
  116. queueMap.Remove(queueName);
  117. }
  118. tokenMap.Add(messageType, token);
  119. queueMap.Add(queueName, queue);
  120. }
  121. }
  122. BatchReceiveMessageResponse batchReceiveMessageResponse = queue.BatchReceiveMessage(16);
  123. List<Message> messages = batchReceiveMessageResponse.Messages;
  124. for (int i = 0; i <= messages.Count - 1; i++)
  125. {
  126. try
  127. {
  128. byte[] outputb = Convert.FromBase64String(messages[i].Body);
  129. string orgStr = Encoding.UTF8.GetString(outputb);
  130. System.Console.WriteLine(orgStr);
  131. //TODO
  132. queue.DeleteMessage(messages[i].ReceiptHandle);
  133. }
  134. catch (Exception e)
  135. {
  136. System.Console.WriteLine("Message Body exception", e);
  137. }
  138. }
  139. }
  140. catch (Exception e)
  141. {
  142. System.Console.WriteLine("Handle exception", e);
  143. }
  144. Thread.Sleep(sleepTime);
  145. }
  146. }
  147. }
  148. }