日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

腾讯云短信服务使用记录与.NET Core C#代码分享

發(fā)布時(shí)間:2023/12/4 C# 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 腾讯云短信服务使用记录与.NET Core C#代码分享 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、即使是相同的短信簽名與短信正文模板,也需要針對(duì)“國(guó)內(nèi)文本短信”與“海外文本短信”分別申請(qǐng)。開始不知道,以為只要申請(qǐng)一次,給國(guó)外手機(jī)發(fā)短信時(shí)給api傳對(duì)應(yīng)的國(guó)家碼就行,后來(lái)才發(fā)現(xiàn)需要分別申請(qǐng)。

2、短信服務(wù)web api響應(yīng)“手機(jī)號(hào)內(nèi)容頻率限制”錯(cuò)誤。這是由于在30秒內(nèi)向同一手機(jī)號(hào)多次發(fā)送了相同內(nèi)容的短信,這是騰訊云短信服務(wù)的默認(rèn)限制——“相同內(nèi)容短信對(duì)同一個(gè)手機(jī)號(hào),30秒內(nèi)發(fā)送短信條數(shù)不超過(guò)1條”,可以通過(guò)“應(yīng)用配置”的“短信頻率配置”修改這個(gè)限制。

3、騰訊云短信服務(wù)沒有提供 .NET Core 的 SDK,我們自己實(shí)現(xiàn)的代碼如下:

public class TencentCloudSmsService : ISmsService

{

? ? private static readonly HttpClient _httpClient =?

? ? ? ? new HttpClient { BaseAddress = new Uri("https://yun.tim.qq.com") };

? ? private readonly string _appId;

? ? private readonly string _appKey;

? ? private const string SIGNATURE = "...";

? ? private const int DOMESTIC_TEMPLATE_ID = 1234;

? ? private const int OVERSEA_TEMPLATE_ID = 5678;

? ? private readonly ILogger _logger;


? ? public TencentCloudSmsService(IConfiguration conf,

? ? ? ? ILoggerFactory loggerFactory)

? ? {

? ? ? ? _appId = conf["tencentCloudSms:appId"];

? ? ? ? if (string.IsNullOrEmpty(_appId))

? ? ? ? ? ? throw new ArgumentException($"{nameof(_appId)} must have a value");


? ? ? ? _appKey = conf["tencentCloudSms:appKey"];

? ? ? ? if (string.IsNullOrEmpty(_appKey))

? ? ? ? ? ? throw new ArgumentException($"{nameof(_appKey)} must have a value");


? ? ? ? _logger = loggerFactory.CreateLogger<TencentCloudSmsService>();

? ? }


? ? public async Task<bool> SendCode(string countryCode, long mobile, int code)

? ? {

? ? ? ? var random = GetRandom();

? ? ? ? var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();

? ? ? ? var data = new

? ? ? ? {

? ? ? ? ? ? tel = new { nationcode = countryCode.Replace("+", ""), mobile = mobile.ToString() },

? ? ? ? ? ? sign = SIGNATURE,

? ? ? ? ? ? tpl_id = countryCode == "+86" ? DOMESTIC_TEMPLATE_ID : OVERSEA_TEMPLATE_ID ,

? ? ? ? ? ? @params = new[] { code.ToString() },

? ? ? ? ? ? sig = ComputeSignature(mobile, random, timestamp),

? ? ? ? ? ? time = timestamp,

? ? ? ? ? ? extend = "",

? ? ? ? ? ? ext = ""

? ? ? ? };


? ? ? ? var url = $"/v5/tlssmssvr/sendsms?sdkappid={_appId}&random={random}";

? ? ? ? _logger.LogDebug("Post to " + _httpClient.BaseAddress + url);

? ? ? ? var response = await _httpClient.PostAsJsonAsync<dynamic>(url, data);

? ? ? ? _logger.LogDebug("Post data:\n" + JsonConvert.SerializeObject(data));

? ? ? ? response.EnsureSuccessStatusCode();


? ? ? ? var result = await response.Content.ReadAsAsync<dynamic>();

? ? ? ? if(result.result != 0)

? ? ? ? {

? ? ? ? ? ? _logger.LogError($"Failed to send message to {countryCode}-{mobile}: {result.errmsg}"); ? ? ? ? ? ? ? ?

? ? ? ? ? ? return false;

? ? ? ? }


? ? ? ? return true;

? ? }


? ? private string ComputeSignature(long mobile, int random, long timestamp)

? ? {

? ? ? ? var input = $"appkey={_appKey}&random={random}&time={timestamp}&mobile={mobile}";

? ? ? ? var hasBytes = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(input));

? ? ? ? return string.Join("", hasBytes.Select(b => b.ToString("x2")));

? ? }


? ? private int GetRandom()

? ? {

? ? ? ? return new Random().Next(100000, 999999);

? ? }

}

原文地址:http://www.cnblogs.com/dudu/p/7782376.html


.NET社區(qū)新聞,深度好文,歡迎訪問(wèn)公眾號(hào)文章匯總 http://www.csharpkit.com

總結(jié)

以上是生活随笔為你收集整理的腾讯云短信服务使用记录与.NET Core C#代码分享的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。