|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+using CallCenter.Utility;
|
|
|
2
|
+using CallCenterApi.Common;
|
|
|
3
|
+using CallCenterApi.Interface.Controllers.Base;
|
|
|
4
|
+using System;
|
|
|
5
|
+using System.Collections.Generic;
|
|
|
6
|
+using System.Data;
|
|
|
7
|
+using System.Linq;
|
|
|
8
|
+using System.Web;
|
|
|
9
|
+using System.Web.Mvc;
|
|
|
10
|
+
|
|
|
11
|
+using System.Net.Http;
|
|
|
12
|
+using System.Security.Cryptography;
|
|
|
13
|
+using System.Text;
|
|
|
14
|
+using System.Threading.Tasks;
|
|
|
15
|
+using Newtonsoft.Json;
|
|
|
16
|
+using TencentCloud.Common;
|
|
|
17
|
+using TencentCloud.Common.Profile;
|
|
|
18
|
+
|
|
|
19
|
+
|
|
|
20
|
+
|
|
|
21
|
+
|
|
|
22
|
+using TencentCloud.Sms.V20210111;
|
|
|
23
|
+using TencentCloud.Sms.V20210111.Models;
|
|
|
24
|
+
|
|
|
25
|
+namespace CallCenterApi.Interface.Controllers
|
|
|
26
|
+{
|
|
|
27
|
+ public class TencentSmsController : BaseController
|
|
|
28
|
+ {
|
|
|
29
|
+
|
|
|
30
|
+
|
|
|
31
|
+ Log log = LogFactory.GetLogger("TencentSmsController");
|
|
|
32
|
+ // private static readonly string secretId = "1400467981";
|
|
|
33
|
+ private static readonly string secretKey = "sZ5eDakngNrodJpQtHgDapyueDcpriQC";
|
|
|
34
|
+ //private static readonly string secretKey = "ba3aa1a33da088dac57bcac8ce5f8d41";
|
|
|
35
|
+ private static readonly string endpoint = "sms.tencentcloudapi.com";
|
|
|
36
|
+ private static readonly string smsSdkAppId = "1400467981";
|
|
|
37
|
+ private static readonly string signName = "巩义市网络舆情研究中心";
|
|
|
38
|
+ private static readonly string templateId = "1963450";
|
|
|
39
|
+ private static readonly string action = "SendSms";
|
|
|
40
|
+ public static string SHA256Hex(string s)
|
|
|
41
|
+ {
|
|
|
42
|
+ using (SHA256 algo = SHA256.Create())
|
|
|
43
|
+ {
|
|
|
44
|
+ byte[] hashbytes = algo.ComputeHash(Encoding.UTF8.GetBytes(s));
|
|
|
45
|
+ StringBuilder builder = new StringBuilder();
|
|
|
46
|
+ for (int i = 0; i < hashbytes.Length; ++i)
|
|
|
47
|
+ {
|
|
|
48
|
+ builder.Append(hashbytes[i].ToString("x2"));
|
|
|
49
|
+ }
|
|
|
50
|
+ return builder.ToString();
|
|
|
51
|
+ }
|
|
|
52
|
+ }
|
|
|
53
|
+
|
|
|
54
|
+ public static byte[] HmacSHA256(byte[] key, byte[] msg)
|
|
|
55
|
+ {
|
|
|
56
|
+ using (HMACSHA256 mac = new HMACSHA256(key))
|
|
|
57
|
+ {
|
|
|
58
|
+ return mac.ComputeHash(msg);
|
|
|
59
|
+ }
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
|
62
|
+ public static Dictionary<String, String> BuildHeaders(string secretid,
|
|
|
63
|
+ string secretkey, string service, string endpoint, string region,
|
|
|
64
|
+ string action, string version, DateTime date, string requestPayload)
|
|
|
65
|
+ {
|
|
|
66
|
+ string datestr = date.ToString("yyyy-MM-dd");
|
|
|
67
|
+ DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
|
|
68
|
+ long requestTimestamp = (long)Math.Round((date - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero) / 1000;
|
|
|
69
|
+ // ************* 步骤 1:拼接规范请求串 *************
|
|
|
70
|
+ string algorithm = "TC3-HMAC-SHA256";
|
|
|
71
|
+ string httpRequestMethod = "POST";
|
|
|
72
|
+ string canonicalUri = "/";
|
|
|
73
|
+ string canonicalQueryString = "";
|
|
|
74
|
+ string contentType = "application/json";
|
|
|
75
|
+ string canonicalHeaders = "content-type:" + contentType + "; charset=utf-8\n" + "host:" + endpoint + "\n";
|
|
|
76
|
+ string signedHeaders = "content-type;host";
|
|
|
77
|
+ string hashedRequestPayload = SHA256Hex(requestPayload);
|
|
|
78
|
+ string canonicalRequest = httpRequestMethod + "\n"
|
|
|
79
|
+ + canonicalUri + "\n"
|
|
|
80
|
+ + canonicalQueryString + "\n"
|
|
|
81
|
+ + canonicalHeaders + "\n"
|
|
|
82
|
+ + signedHeaders + "\n"
|
|
|
83
|
+ + hashedRequestPayload;
|
|
|
84
|
+ Console.WriteLine(canonicalRequest);
|
|
|
85
|
+
|
|
|
86
|
+ // ************* 步骤 2:拼接待签名字符串 *************
|
|
|
87
|
+ string credentialScope = datestr + "/" + service + "/" + "tc3_request";
|
|
|
88
|
+ string hashedCanonicalRequest = SHA256Hex(canonicalRequest);
|
|
|
89
|
+ string stringToSign = algorithm + "\n" + requestTimestamp.ToString() + "\n" + credentialScope + "\n" + hashedCanonicalRequest;
|
|
|
90
|
+ Console.WriteLine(stringToSign);
|
|
|
91
|
+
|
|
|
92
|
+ // ************* 步骤 3:计算签名 *************
|
|
|
93
|
+ byte[] tc3SecretKey = Encoding.UTF8.GetBytes("TC3" + secretkey);
|
|
|
94
|
+ byte[] secretDate = HmacSHA256(tc3SecretKey, Encoding.UTF8.GetBytes(datestr));
|
|
|
95
|
+ byte[] secretService = HmacSHA256(secretDate, Encoding.UTF8.GetBytes(service));
|
|
|
96
|
+ byte[] secretSigning = HmacSHA256(secretService, Encoding.UTF8.GetBytes("tc3_request"));
|
|
|
97
|
+ byte[] signatureBytes = HmacSHA256(secretSigning, Encoding.UTF8.GetBytes(stringToSign));
|
|
|
98
|
+ string signature = BitConverter.ToString(signatureBytes).Replace("-", "").ToLower();
|
|
|
99
|
+ Console.WriteLine(signature);
|
|
|
100
|
+
|
|
|
101
|
+ // ************* 步骤 4:拼接 Authorization *************
|
|
|
102
|
+ string authorization = algorithm + " "
|
|
|
103
|
+ + "Credential=" + secretid + "/" + credentialScope + ", "
|
|
|
104
|
+ + "SignedHeaders=" + signedHeaders + ", "
|
|
|
105
|
+ + "Signature=" + signature;
|
|
|
106
|
+ Console.WriteLine(authorization);
|
|
|
107
|
+
|
|
|
108
|
+ Dictionary<string, string> headers = new Dictionary<string, string>();
|
|
|
109
|
+ headers.Add("Authorization", authorization);
|
|
|
110
|
+ headers.Add("Host", endpoint);
|
|
|
111
|
+ headers.Add("Content-Type", contentType + "; charset=utf-8");
|
|
|
112
|
+ headers.Add("X-TC-Timestamp", requestTimestamp.ToString());
|
|
|
113
|
+ headers.Add("X-TC-Version", version);
|
|
|
114
|
+ headers.Add("X-TC-Action", action);
|
|
|
115
|
+ headers.Add("X-TC-Region", region);
|
|
|
116
|
+ return headers;
|
|
|
117
|
+ }
|
|
|
118
|
+
|
|
|
119
|
+
|
|
|
120
|
+ // public static async Task SendSmsAsync(string phoneNumber, string[] templateParams)
|
|
|
121
|
+ // {
|
|
|
122
|
+ // var request = new
|
|
|
123
|
+ // {
|
|
|
124
|
+ // SmsSdkAppId = smsSdkAppId,
|
|
|
125
|
+ // Sign = signName,
|
|
|
126
|
+ // TemplateId = templateId,
|
|
|
127
|
+ // PhoneNumberSet = new[] { phoneNumber },
|
|
|
128
|
+ // TemplateParamSet = templateParams
|
|
|
129
|
+ // };
|
|
|
130
|
+
|
|
|
131
|
+ // var requestBody = JsonConvert.SerializeObject(request);
|
|
|
132
|
+ // var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
|
|
|
133
|
+ // var nonce = new Random().Next(100000, 999999).ToString();
|
|
|
134
|
+ // var signature = GenerateSignature(requestBody, timestamp, nonce);
|
|
|
135
|
+
|
|
|
136
|
+ // using (var client = new HttpClient())
|
|
|
137
|
+ // {
|
|
|
138
|
+ // client.DefaultRequestHeaders.Add("Authorization", signature);
|
|
|
139
|
+ // client.DefaultRequestHeaders.Add("X-TC-Action", "SendSms");
|
|
|
140
|
+ // client.DefaultRequestHeaders.Add("X-TC-Timestamp", timestamp);
|
|
|
141
|
+ // client.DefaultRequestHeaders.Add("X-TC-Nonce", nonce);
|
|
|
142
|
+ // client.DefaultRequestHeaders.Add("X-TC-Version", "2021-01-11");
|
|
|
143
|
+
|
|
|
144
|
+ // //foreach (KeyValuePair<string, string> kv in headers)
|
|
|
145
|
+ // //{
|
|
|
146
|
+ // // // Console.WriteLine(kv.Key + ": " + kv.Value);
|
|
|
147
|
+ // // client.DefaultRequestHeaders.Add(kv.Key, kv.Value);
|
|
|
148
|
+ // //}
|
|
|
149
|
+
|
|
|
150
|
+ // var content = new StringContent(requestBody, Encoding.UTF8, "application/json");
|
|
|
151
|
+ // var response = await client.PostAsync($"https://{endpoint}", content);
|
|
|
152
|
+
|
|
|
153
|
+ // if (response.IsSuccessStatusCode)
|
|
|
154
|
+ // {
|
|
|
155
|
+ // var responseBody = await response.Content.ReadAsStringAsync();
|
|
|
156
|
+ // Console.WriteLine("SMS sent successfully: " + responseBody);
|
|
|
157
|
+ // }
|
|
|
158
|
+ // else
|
|
|
159
|
+ // {
|
|
|
160
|
+ // Console.WriteLine("Failed to send SMS: " + response.ReasonPhrase);
|
|
|
161
|
+ // }
|
|
|
162
|
+ // }
|
|
|
163
|
+ // }
|
|
|
164
|
+
|
|
|
165
|
+ // private static string GenerateSignature(string requestBody, string timestamp, string nonce)
|
|
|
166
|
+ // {
|
|
|
167
|
+ // var source = $"POST{smsSdkAppId}{timestamp}{nonce}{requestBody}";
|
|
|
168
|
+ // using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey)))
|
|
|
169
|
+ // {
|
|
|
170
|
+ // var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(source));
|
|
|
171
|
+ // return Convert.ToBase64String(hash);
|
|
|
172
|
+ // }
|
|
|
173
|
+ // }
|
|
|
174
|
+ // public static byte[] HmacSHA256(byte[] key, byte[] msg)
|
|
|
175
|
+ // {
|
|
|
176
|
+ // using (HMACSHA256 mac = new HMACSHA256(key))
|
|
|
177
|
+ // {
|
|
|
178
|
+ // return mac.ComputeHash(msg);
|
|
|
179
|
+ // }
|
|
|
180
|
+ // }
|
|
|
181
|
+ // public static string SHA256Hex(string s)
|
|
|
182
|
+ // {
|
|
|
183
|
+ // using (SHA256 algo = SHA256.Create())
|
|
|
184
|
+ // {
|
|
|
185
|
+ // byte[] hashbytes = algo.ComputeHash(Encoding.UTF8.GetBytes(s));
|
|
|
186
|
+ // StringBuilder builder = new StringBuilder();
|
|
|
187
|
+ // for (int i = 0; i < hashbytes.Length; ++i)
|
|
|
188
|
+ // {
|
|
|
189
|
+ // builder.Append(hashbytes[i].ToString("x2"));
|
|
|
190
|
+ // }
|
|
|
191
|
+ // return builder.ToString();
|
|
|
192
|
+ // }
|
|
|
193
|
+ // }
|
|
|
194
|
+
|
|
|
195
|
+ // public static Dictionary<String, String> BuildHeaders(string secretid,
|
|
|
196
|
+ // string secretkey, string service, string endpoint, string region,
|
|
|
197
|
+ // string action, string version, DateTime date, string requestPayload) {
|
|
|
198
|
+
|
|
|
199
|
+
|
|
|
200
|
+ // string datestr = date.ToString("yyyy-MM-dd");
|
|
|
201
|
+ // DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
|
|
202
|
+ // long requestTimestamp = (long)Math.Round((date - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero) / 1000;
|
|
|
203
|
+ // // ************* 步骤 1:拼接规范请求串 *************
|
|
|
204
|
+ // string algorithm = "TC3-HMAC-SHA256";
|
|
|
205
|
+ // string httpRequestMethod = "POST";
|
|
|
206
|
+ // string canonicalUri = "/";
|
|
|
207
|
+ // string canonicalQueryString = "";
|
|
|
208
|
+ // string contentType = "application/json";
|
|
|
209
|
+ // string canonicalHeaders = "content-type:" + contentType + "; charset=utf-8\n"
|
|
|
210
|
+ // + "host:" + endpoint + "\n"
|
|
|
211
|
+ // + "x-tc-action:" + action.ToLower() + "\n";
|
|
|
212
|
+ // string signedHeaders = "content-type;host;x-tc-action";
|
|
|
213
|
+ // string hashedRequestPayload = SHA256Hex(requestPayload);
|
|
|
214
|
+ // string canonicalRequest = httpRequestMethod + "\n"
|
|
|
215
|
+ // + canonicalUri + "\n"
|
|
|
216
|
+ // + canonicalQueryString + "\n"
|
|
|
217
|
+ // + canonicalHeaders + "\n"
|
|
|
218
|
+ // + signedHeaders + "\n"
|
|
|
219
|
+ // + hashedRequestPayload;
|
|
|
220
|
+ // Console.WriteLine(canonicalRequest);
|
|
|
221
|
+
|
|
|
222
|
+ // // ************* 步骤 2:拼接待签名字符串 *************
|
|
|
223
|
+ // string credentialScope = datestr + "/" + service + "/" + "tc3_request";
|
|
|
224
|
+ // string hashedCanonicalRequest = SHA256Hex(canonicalRequest);
|
|
|
225
|
+ // string stringToSign = algorithm + "\n"
|
|
|
226
|
+ // + requestTimestamp.ToString() + "\n"
|
|
|
227
|
+ // + credentialScope + "\n"
|
|
|
228
|
+ // + hashedCanonicalRequest;
|
|
|
229
|
+ // Console.WriteLine(stringToSign);
|
|
|
230
|
+
|
|
|
231
|
+ // // ************* 步骤 3:计算签名 *************
|
|
|
232
|
+ // byte[] tc3SecretKey = Encoding.UTF8.GetBytes("TC3" + secretkey);
|
|
|
233
|
+ // byte[] secretDate = HmacSHA256(tc3SecretKey, Encoding.UTF8.GetBytes(datestr));
|
|
|
234
|
+ // byte[] secretService = HmacSHA256(secretDate, Encoding.UTF8.GetBytes(service));
|
|
|
235
|
+ // byte[] secretSigning = HmacSHA256(secretService, Encoding.UTF8.GetBytes("tc3_request"));
|
|
|
236
|
+ // byte[] signatureBytes = HmacSHA256(secretSigning, Encoding.UTF8.GetBytes(stringToSign));
|
|
|
237
|
+ // string signature = BitConverter.ToString(signatureBytes).Replace("-", "").ToLower();
|
|
|
238
|
+ // Console.WriteLine(signature);
|
|
|
239
|
+
|
|
|
240
|
+ // // ************* 步骤 4:拼接 Authorization *************
|
|
|
241
|
+ // string authorization = algorithm + " "
|
|
|
242
|
+ // + "Credential=" + secretid + "/" + credentialScope + ", "
|
|
|
243
|
+ // + "SignedHeaders=" + signedHeaders + ", "
|
|
|
244
|
+ // + "Signature=" + signature;
|
|
|
245
|
+ // Console.WriteLine(authorization);
|
|
|
246
|
+ // Dictionary<string, string> headers = new Dictionary<string, string>();
|
|
|
247
|
+ // headers.Add("Authorization", authorization);
|
|
|
248
|
+ // headers.Add("Host", endpoint);
|
|
|
249
|
+ // headers.Add("Content-Type", contentType + "; charset=utf-8");
|
|
|
250
|
+ // headers.Add("X-TC-Timestamp", requestTimestamp.ToString());
|
|
|
251
|
+ // headers.Add("X-TC-Version", version);
|
|
|
252
|
+ // headers.Add("X-TC-Action", action);
|
|
|
253
|
+ // headers.Add("X-TC-Region", region);
|
|
|
254
|
+ // return headers;
|
|
|
255
|
+ // }
|
|
|
256
|
+
|
|
|
257
|
+ [HttpGet]
|
|
|
258
|
+ public void SmsNew()
|
|
|
259
|
+ {
|
|
|
260
|
+ //string phoneNumber = "15639002755"; // 替换为实际的手机号码
|
|
|
261
|
+ //var templateParams = new[] { "202402110000" }; // 替换为实际的模板参数
|
|
|
262
|
+ //string service = "cvm";
|
|
|
263
|
+ //string endpoint = "cvm.tencentcloudapi.com";
|
|
|
264
|
+ //string region = "ap-guangzhou";
|
|
|
265
|
+ //string action = "DescribeInstances";
|
|
|
266
|
+ //string version = "2017-03-12";
|
|
|
267
|
+ //DateTime date = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(1551113065);
|
|
|
268
|
+
|
|
|
269
|
+
|
|
|
270
|
+ // string requestPayload = "{\"Limit\": 1, \"Filters\": [{\"Values\": [\"\\u672a\\u547d\\u540d\"], \"Name\": \"instance-name\"}]}";
|
|
|
271
|
+
|
|
|
272
|
+
|
|
|
273
|
+ // Dictionary<string, string> headers = BuildHeaders(smsSdkAppId, secretKey, service
|
|
|
274
|
+ //, endpoint, region, action, version, date);
|
|
|
275
|
+ //string SECRET_ID = "AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******";
|
|
|
276
|
+ //string SECRET_KEY = "Gu5t9xGARNpq86cd98joQYCN3*******";
|
|
|
277
|
+
|
|
|
278
|
+ string service = "sms";
|
|
|
279
|
+ string endpoint = "sms.tencentcloudapi.com";
|
|
|
280
|
+ string region = "ap-guangzhou";
|
|
|
281
|
+ string action = "SendSms";
|
|
|
282
|
+ string version = "2021-01-11";
|
|
|
283
|
+
|
|
|
284
|
+ // 此处由于示例规范的原因,采用时间戳2019-02-26 00:44:25,此参数作为示例,如果在项目中,您应当使用:
|
|
|
285
|
+ DateTime date = DateTime.UtcNow;
|
|
|
286
|
+ // 注意时区,建议此时间统一采用UTC时间戳,否则容易出错
|
|
|
287
|
+ // DateTime date = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(1551113065);
|
|
|
288
|
+ // 实际调用需要更新参数,这里仅作为演示签名验证通过的例子
|
|
|
289
|
+ string requestPayload = "{\"SmsSdkAppId\":\"1400467981\",\"TemplateParamSet\":[\"11111230\"],\"PhoneNumberSet\":[\"+8615639002755\"],\"SessionContext\":\"test\",\"SignName\":\"巩义市网络舆情研究中心\",\"TemplateId\":\"1963450\"}";
|
|
|
290
|
+
|
|
|
291
|
+ Dictionary<string, string> headers = BuildHeaders(smsSdkAppId, secretKey, service
|
|
|
292
|
+ , endpoint, region, action, version, date, requestPayload);
|
|
|
293
|
+
|
|
|
294
|
+ Console.WriteLine("POST https://sms.tencentcloudapi.com");
|
|
|
295
|
+ foreach (KeyValuePair<string, string> kv in headers)
|
|
|
296
|
+ {
|
|
|
297
|
+ Console.WriteLine(kv.Key + ": " + kv.Value);
|
|
|
298
|
+ }
|
|
|
299
|
+ Console.WriteLine();
|
|
|
300
|
+ Console.WriteLine(requestPayload);
|
|
|
301
|
+
|
|
|
302
|
+ string curlcmd = "curl -X POST https://" + endpoint;
|
|
|
303
|
+ foreach (KeyValuePair<string, string> kv in headers)
|
|
|
304
|
+ {
|
|
|
305
|
+ curlcmd = curlcmd + " -H \"" + kv.Key + ": " + kv.Value + "\"";
|
|
|
306
|
+ }
|
|
|
307
|
+ curlcmd = curlcmd + " -d '" + requestPayload + "'";
|
|
|
308
|
+ Console.WriteLine(curlcmd);
|
|
|
309
|
+ }
|
|
|
310
|
+
|
|
|
311
|
+ public void SmsAsync () {
|
|
|
312
|
+
|
|
|
313
|
+ try
|
|
|
314
|
+ {
|
|
|
315
|
+ // 必要步骤:
|
|
|
316
|
+ // 实例化一个认证对象,入参需要传入腾讯云账户密钥对 SecretId,SecretKey。
|
|
|
317
|
+ // 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。
|
|
|
318
|
+ // 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
|
|
|
319
|
+ // 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
|
|
|
320
|
+ // SecretId、SecretKey 查询:https://console.cloud.tencent.com/cam/capi
|
|
|
321
|
+ Credential cred = new Credential
|
|
|
322
|
+ {
|
|
|
323
|
+ SecretId = "AKIDugJkz1qSq21oPrAHH0Zw63rSq33tb9VQ",// Environment.GetEnvironmentVariable("TENCENTCLOUD_SECRET_ID"),
|
|
|
324
|
+ SecretKey = secretKey// Environment.GetEnvironmentVariable("TENCENTCLOUD_SECRET_KEY")
|
|
|
325
|
+ };
|
|
|
326
|
+
|
|
|
327
|
+ /* 非必要步骤:
|
|
|
328
|
+ * 实例化一个客户端配置对象,可以指定超时时间等配置 */
|
|
|
329
|
+ ClientProfile clientProfile = new ClientProfile();
|
|
|
330
|
+ /* SDK 默认用TC3-HMAC-SHA256进行签名
|
|
|
331
|
+ * 非必要请不要修改这个字段 */
|
|
|
332
|
+ clientProfile.SignMethod = ClientProfile.SIGN_TC3SHA256;
|
|
|
333
|
+ /* 非必要步骤
|
|
|
334
|
+ * 实例化一个客户端配置对象,可以指定超时时间等配置 */
|
|
|
335
|
+ HttpProfile httpProfile = new HttpProfile();
|
|
|
336
|
+ /* SDK默认使用POST方法。
|
|
|
337
|
+ * 如果您一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */
|
|
|
338
|
+ httpProfile.ReqMethod = "POST";
|
|
|
339
|
+ httpProfile.Timeout = 10; // 请求连接超时时间,单位为秒(默认60秒)
|
|
|
340
|
+ /* 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com */
|
|
|
341
|
+ httpProfile.Endpoint = "sms.tencentcloudapi.com";
|
|
|
342
|
+ // 代理服务器,当您的环境下有代理服务器时设定(无需要直接忽略)
|
|
|
343
|
+ // httpProfile.WebProxy = Environment.GetEnvironmentVariable("HTTPS_PROXY");
|
|
|
344
|
+
|
|
|
345
|
+ clientProfile.HttpProfile = httpProfile;
|
|
|
346
|
+ /* 实例化要请求产品(以sms为例)的client对象
|
|
|
347
|
+ * 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8 */
|
|
|
348
|
+ SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
|
|
|
349
|
+
|
|
|
350
|
+ /* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
|
|
|
351
|
+ * 您可以直接查询SDK源码确定SendSmsRequest有哪些属性可以设置
|
|
|
352
|
+ * 属性可能是基本类型,也可能引用了另一个数据结构
|
|
|
353
|
+ * 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
|
|
|
354
|
+ SendSmsRequest req = new SendSmsRequest();
|
|
|
355
|
+
|
|
|
356
|
+ /* 基本类型的设置:
|
|
|
357
|
+ * SDK采用的是指针风格指定参数,即使对于基本类型您也需要用指针来对参数赋值。
|
|
|
358
|
+ * SDK提供对基本类型的指针引用封装函数
|
|
|
359
|
+ * 帮助链接:
|
|
|
360
|
+ * 短信控制台: https://console.cloud.tencent.com/smsv2
|
|
|
361
|
+ * 腾讯云短信小助手: https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81 */
|
|
|
362
|
+ /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
|
|
|
363
|
+ // 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
|
|
|
364
|
+ req.SmsSdkAppId = "1400467981";// "1304639164";
|
|
|
365
|
+
|
|
|
366
|
+ /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
|
|
|
367
|
+ // 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
|
|
|
368
|
+ req.SignName = signName;
|
|
|
369
|
+
|
|
|
370
|
+ /* 模板 ID: 必须填写已审核通过的模板 ID */
|
|
|
371
|
+ // 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
|
|
|
372
|
+ req.TemplateId = templateId;// "449739";
|
|
|
373
|
+
|
|
|
374
|
+ /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */
|
|
|
375
|
+ req.TemplateParamSet = new String[] { "DH20250808996" };
|
|
|
376
|
+
|
|
|
377
|
+ /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
|
|
|
378
|
+ * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
|
|
|
379
|
+ req.PhoneNumberSet = new String[] { "15639002755","17839539537" };
|
|
|
380
|
+
|
|
|
381
|
+ /* 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
|
|
|
382
|
+ //req.SessionContext = "";
|
|
|
383
|
+
|
|
|
384
|
+ /* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手] */
|
|
|
385
|
+ //req.ExtendCode = "";
|
|
|
386
|
+
|
|
|
387
|
+ /* 国内短信无需填写该项;国际/港澳台短信已申请独立 SenderId 需要填写该字段,默认使用公共 SenderId,无需填写该字段。注:月度使用量达到指定量级可申请独立 SenderId 使用,详情请联系 [腾讯云短信小助手](https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81)。 */
|
|
|
388
|
+ //req.SenderId = "";
|
|
|
389
|
+
|
|
|
390
|
+ SendSmsResponse resp = client.SendSmsSync(req);
|
|
|
391
|
+
|
|
|
392
|
+ // 输出json格式的字符串回包
|
|
|
393
|
+ // Console.WriteLine(AbstractModel.ToJsonString(resp));
|
|
|
394
|
+
|
|
|
395
|
+ /* 当出现以下错误码时,快速解决方案参考
|
|
|
396
|
+ * [FailedOperation.SignatureIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.signatureincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
|
|
|
397
|
+ * [FailedOperation.TemplateIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.templateincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
|
|
|
398
|
+ * [UnauthorizedOperation.SmsSdkAppIdVerifyFail](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunauthorizedoperation.smssdkappidverifyfail-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
|
|
|
399
|
+ * [UnsupportedOperation.ContainDomesticAndInternationalPhoneNumber](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunsupportedoperation.containdomesticandinternationalphonenumber-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
|
|
|
400
|
+ * 更多错误,可咨询[腾讯云助手](https://tccc.qcloud.com/web/im/index.html#/chat?webAppId=8fa15978f85cb41f7e2ea36920cb3ae1&title=Sms)
|
|
|
401
|
+ */
|
|
|
402
|
+ }
|
|
|
403
|
+ catch (Exception e)
|
|
|
404
|
+ {
|
|
|
405
|
+ log.Error(e.Message);
|
|
|
406
|
+ // Console.WriteLine(e.ToString());
|
|
|
407
|
+ }
|
|
|
408
|
+ Console.Read();
|
|
|
409
|
+ }
|
|
|
410
|
+ }
|
|
|
411
|
+ }
|
|
|
412
|
+
|
|
|
413
|
+
|
|
|
414
|
+
|