NetCore 阿里大于发送短信
使用阿里大于API发送短信,但阿里没有提供NetCore 的API,自己看了下源码重写了发短信这个部分
public class MessageSender
{
private readonly string _appKey;
private readonly string _appSecret;
private readonly string _serverUrl; public MessageSender(string url, string appKey, string appSecret)
{
_serverUrl = url;
_appKey = appKey;
_appSecret = appSecret;
} public string SmsType { get; set; } = "normal";
public string SmsFreeSignName { get; set; } = "生日提醒";
public string SmsParam { get; set; }
public string RecNum { get; set; }
public string SmsTemplateCode { get; set; } = "SMS_24695114"; public string GetApiName()
{
return "alibaba.aliqin.fc.sms.num.send";
} public string SendMessage()
{
HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip");
//httpClient.DefaultRequestHeaders.Add("KeepAlive", "true");
httpClient.DefaultRequestHeaders.Add("user-agent", "top-sdk-net");
httpClient.DefaultRequestHeaders.Add("Method", "Post");
httpClient.DefaultRequestHeaders.Add("Accept", "text/xml,text/javascript");
httpClient.DefaultRequestHeaders.Add("Host", "gw.api.taobao.com");
httpClient.Timeout = new TimeSpan(, , ); byte[] postData = GetPostData(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, _serverUrl);
request.Headers.Add("Accept-Encoding", "gzip");
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8"));
request.Content = new StreamContent(new MemoryStream(postData));
request.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"); HttpResponseMessage httpResponseMessage = httpClient.SendAsync(request).GetAwaiter().GetResult();
string result = httpResponseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult(); return result;
} public byte[] GetPostData()
{
Dictionary<string, string> dictionary = new Dictionary<string, string>(); dictionary.Add("rec_num", RecNum);
dictionary.Add("sms_free_sign_name", SmsFreeSignName);
dictionary.Add("sms_param", SmsParam);
dictionary.Add("sms_template_code", SmsTemplateCode);
dictionary.Add("sms_type", SmsType);
dictionary.Add("method", "alibaba.aliqin.fc.sms.num.send");
dictionary.Add("v", "2.0");
dictionary.Add("sign_method", "hmac");
dictionary.Add("app_key", _appKey);
dictionary.Add("format", "xml");
dictionary.Add("partner_id", "top-sdk-net-20160607");
dictionary.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
//dictionary.Add("target_app_key", null);
//dictionary.Add("session", null);
dictionary.Add("sign", SignTopRequest(dictionary, null, _appSecret, "hmac")); byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(dictionary)); return postData;
} public string SignTopRequest(IDictionary<string, string> parameters, string body, string secret, string signMethod)
{
// 第一步:把字典按Key的字母顺序排序
IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters, StringComparer.Ordinal); // 第二步:把所有参数名和参数值串在一起
StringBuilder query = new StringBuilder();
if ("md5".Equals(signMethod))
{
query.Append(secret);
}
foreach (KeyValuePair<string, string> kv in sortedParams)
{
if (!string.IsNullOrEmpty(kv.Key) && !string.IsNullOrEmpty(kv.Value))
{
query.Append(kv.Key).Append(kv.Value);
}
} // 第三步:把请求主体拼接在参数后面
if (!string.IsNullOrEmpty(body))
{
query.Append(body);
} // 第四步:使用MD5/HMAC加密
byte[] bytes;
if ("hmac".Equals(signMethod))
{
HMACMD5 hmac = new HMACMD5(Encoding.UTF8.GetBytes(secret));
bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(query.ToString()));
}
else
{
query.Append(secret);
MD5 md5 = MD5.Create();
bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(query.ToString()));
} // 第五步:把二进制转化为大写的十六进制
StringBuilder result = new StringBuilder();
foreach (byte t in bytes)
{
result.Append(t.ToString("X2"));
} return result.ToString();
} public string BuildQuery(IDictionary<string, string> parameters)
{
if (parameters == null || parameters.Count == )
{
return null;
} StringBuilder query = new StringBuilder();
bool hasParam = false; foreach (KeyValuePair<string, string> kv in parameters)
{
string name = kv.Key;
string value = kv.Value;
// 忽略参数名或参数值为空的参数
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
{
if (hasParam)
{
query.Append("&");
} query.Append(name);
query.Append("=");
query.Append(WebUtility.UrlEncode(value));
hasParam = true;
}
} return query.ToString();
}
}
使用的话
MessageSender messageSender = new MessageSender("http://gw.api.taobao.com/router/rest", "你的APPkey", "你的APPSecret");
messageSender.SmsType = "normal";
messageSender.SmsFreeSignName = "生日提醒";
messageSender.SmsParam = string.Format("{{\"birthday\":\"{0}\",\"SpiritualBirthday\":\"{1}\"}}", birthdayTemplate, spiritualBirthdayTemplate);
messageSender.RecNum = phone;
messageSender.SmsTemplateCode = "你的短信模板";
string result = messageSender.SendMessage();
NetCore 阿里大于发送短信的更多相关文章
- 阿里大于发送短信(java)
一.短信签名设置 1.短信签名是什么? 签名是在短信内容开始或者末尾跟的品牌或者应用名称,设置签名有一下几个好处:增加品牌的曝光度,增强用户的记忆让用户能更清楚的知道正在使用的应用. 2.签名可不可以 ...
- PHP阿里大于发短信教程
PHP阿里大于发短信教程 1 先去控制台 https://www.alidayu.com/center/user/account?spm=a3142.7791109.1999204004.5.ZtBQ ...
- thinkphp 5.0整合阿里大于验证码短信发送接口,含完整模型验证实例DEMO
为大家分享一个阿里大于短信发送接口: 首先创建一个发送模型(Send.php): <?php namespace app\index\model; use think\Validate; cla ...
- 通过sails和阿里大于实现短信验证
通过sails与阿里大于来实现注册短信验证码的发送,逻辑图如下 1.用户在客户端发送手机号给服务器,服务器接收到手机号,生成对应时间戳,随机四位数验证码 2.服务器将电话号码和验证码告诉阿里大于服务器 ...
- Python2-Django配置阿里大于的短信验证码接口
1.短信发送开发指南地址:https://help.aliyun.com/document_detail/55491.html?spm=a2c4g.11186623.6.568.l5zTwH 2.SD ...
- golang sms阿里云发送短信(公司实际项目)
话不多说,直接上代码!!! 要先下载两个包 (可以go get -u +你想要的包) github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests githu ...
- Delphi - Delphi7 调用阿里大于实现短信消息验证
阿里大于是阿里通信旗下产品,融合了三大运营商的通信能力,提供包括短信.语音.流量直充.私密专线.店铺手机号等个性化服务.每条四分五,价钱还算公道,经老农测试,响应速度非常快,基本上是秒到.官方文档提供 ...
- PHP使用阿里大鱼发送短信验证
目前,基本上所有的网站注册都要求手机绑定,并通过下发短信验证码方式验证手机的真实性,提高了用户的真实性.但是一般企业单独申请短信行业通道都比较困难,因此选择一家信誉好,稳定性.及时性强的第三方短信通道 ...
- C# 使用阿里云发送短信
最近有个项目,短信服务使用的是阿里云的,由于时间问题,没有手动去构造sign去发送,而是直接使用阿里云的SDK发送,所以这里算是做个笔记,或许以后还能用得到 首先,我们需要安装阿里云的SDK,推荐使用 ...
随机推荐
- Cocos2d-x游戏引擎实战开发炸弹超人项目教程 全套下载 1至6课
下载地址: http://pan.baidu.com/s/1b19HN
- c#语言-高阶函数
介绍 如果说函数是程序中的基本模块,代码段,那高阶函数就是函数的高阶(级)版本,其基本定义如下: 函数自身接受一个或多个函数作为输入. 函数自身能输出一个函数,即函数生产函数. 满足其中一个条件就可以 ...
- 【Java并发编程实战】-----“J.U.C”:ReentrantLock之二lock方法分析
前一篇博客简单介绍了ReentrantLock的定义和与synchronized的区别,下面跟随LZ的笔记来扒扒ReentrantLock的lock方法.我们知道ReentrantLock有公平锁.非 ...
- 【Prince2是什么】PRINCE2认证之项目四大管理步骤
昨天谈到PRINCE2要求项目经理在做项目的时候要考虑四大核心指标(成本.时间.质量.范围)加风险与收益这两个重要要素. 然后PRINCE2基于这几大要素进行了四大管理步骤,分别是: 1.计划 2.授 ...
- .NET Memory Profiler 查看内存使用情况
1 简介 .Net Memory Profiler(以下简称Profiler):专门针对于.NET程序,功能最全的内存分析工具,最大的特点是具有内存动态分析(Automatic Memory Anal ...
- 浅析z-index(覆盖顺序)和定位
多次在项目中遇到html页面元素的非期待重叠错误,多数还是position定位情况下z-index的问题.其实每次解决类似问题思路大致都是一样的,说到底还是对z-index的理解比较模糊,可以解决问题 ...
- Transaction Replication6:Transaction cleanup
distribution中暂存的Transactions和Commands必须及时cleanup,否则,distribution size会一直增长,最终导致数据更新耗时增加,影响replicatio ...
- 今天不谈技术,说说一些常用的软件~By 逆天
前端工具:http://www.cnblogs.com/dunitian/p/5640147.html 在线办公: http://word.baidu.com/welcome.html http ...
- OpenCASCADE6.8.0 Reference Manual Serach Problem
OpenCASCADE6.8.0 Reference Manual Serach Problem eryar@163.com 1. Problem 有网友反映OpenCASCADE6.8.0的Refe ...
- Plant Design Review Based on AnyCAD
Plant Design Review Based on AnyCAD eryar@163.com Abstract. AVEVA Review is used to 3D model visuali ...