asp.net core 阿里云消息服务(Message Service,原MQS)发送接口的实现

asp.net core 阿里云消息服务(Message Service,原MQS)发送接口的实现最近在后台处理订单统计等相关功能用到了大力的mqs,由于官方没有实现asp.netcore的sdk,这里简单实现了发送信息的功能,有兴趣的可以参考实现其他相关功能usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net.Http;usingSystem.Net.Http.Headers;…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

最近在后台处理订单统计等相关功能用到了大力的mqs,由于官方没有实现asp.net core的sdk,这里简单实现了发送信息的功能,有兴趣的可以参考实现其他相关功能

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Iyibank.Aliyun.MNS
{

public class MQHelper
{

private string url;
private string accessKeyId;
private string accessKeySecret;

private string host;
private string version = “2015-06-06”;

public MQHelper(string url, string accessKeyId, string accessKeySecret)
{

this.url = url;
this.accessKeyId = accessKeyId;
this.accessKeySecret = accessKeySecret;

this.host = url.StartsWith(“http://”) ? url.Substring(7) : url;

}
/// <summary>
/// URL 中的 Key,Tag以及 POST Content-Type 没有任何的限制,只要确保Key 和 Tag 相同唯一即可
/// </summary>
/// <param name=”tag”></param>
/// <param name=”body”></param>
/// <returns></returns>
public async Task<bool> Pub(string name, string body)
{

try
{

using (HttpClient httpClient = new HttpClient())
{

Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add(“Host”, this.host);
headers.Add(“Date”, DateTime.Now.ToUniversalTime().ToString(“r”));
headers.Add(“x-mns-version”, this.version);
headers[“Content-Type”] = “text/xml”;
string url = string.Format(“{0}/{1}”, name, “messages”);
headers.Add(“Authorization”, this.authorization(“POST”, headers, string.Format(“{0}”, “/queues/” + name + “/messages”)));

foreach (var kv in headers)
{

if (kv.Key != “Content-Type”)
{

httpClient.DefaultRequestHeaders.Add(kv.Key, kv.Value);
}

}
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/xml”));
StringBuilder sb = new StringBuilder();
sb.Append(” <Message> “);
sb.Append(“<MessageBody>” + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(body)) + “</MessageBody> “);
sb.Append(“<DelaySeconds>0</DelaySeconds> “);
sb.Append(” <Priority>1</Priority>”);
sb.Append(“</Message>”);
HttpContent content = new StringContent(sb.ToString());
content.Headers.ContentType = new MediaTypeHeaderValue(“text/xml”);
httpClient.DefaultRequestHeaders.Connection.Add(“keep-alive”);
var res = await httpClient.PostAsync(this.url + “/” + string.Format(“queues/{0}/{1}”, name, “messages”), content);
if (res.StatusCode == System.Net.HttpStatusCode.Created)
{

return true;
}
return false;
}
}
catch { return false; }
}
/// <summary>
/// 生成验证信息
/// </summary>
/// <param name=”method”></param>
/// <param name=”headers”></param>
/// <param name=”resource”></param>
/// <returns></returns>
private string authorization(string method, Dictionary<string, string> headers, string resource)
{

return string.Format(“MNS {0}:{1}”, this.accessKeyId, this.signature(“POST”, headers, resource));
}
/// <summary>
/// 签名
/// </summary>
/// <param name=”method”></param>
/// <param name=”headers”></param>
/// <param name=”resource”></param>
/// <returns></returns>
private string signature(string method, Dictionary<string, string> headers, string resource)
{

List<string> toSign = new List<string>();
toSign.Add(method.ToString());
toSign.Add(headers.ContainsKey(“Content-MD5”) ? headers[“Content-MD5”] : string.Empty);
toSign.Add(headers.ContainsKey(“Content-Type”) ? headers[“Content-Type”] : string.Empty);
toSign.Add(headers.ContainsKey(“Date”) ? headers[“Date”] : DateTime.Now.ToUniversalTime().ToString(“r”));

 

foreach (KeyValuePair<string, string> header in headers.Where(kv => kv.Key.StartsWith(“x-mns-“)).OrderBy(kv => kv.Key))
{

toSign.Add(string.Format(“{0}:{1}”, header.Key, header.Value));
}

toSign.Add(resource);

HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(this.accessKeySecret));
string key = string.Join(“\n”, toSign);
var hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(string.Join(“\n”, toSign)));
return Convert.ToBase64String(hashBytes);
}
}
}

转载于:https://www.cnblogs.com/zhangkjun/p/6143381.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/222978.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 在线体验流媒体服务器软件系统 (密码:123456)

    在线体验流媒体服务器软件系统 (密码:123456)无需下载体验,无需注册,无需费用,直接点击进入体验流媒体服务器直播,点播。感受八百里流媒体FlashP2P技术的先进。 流媒体服务器缩略图:如何在线体验:http://www.800li.net:8085密码:123123网站前台示例:http://www.800li.net/phpvod/或:www.ycitv.org/video

    2022年5月10日
    41
  • 微信扫码登陆(1)—扫码登录流程讲解、获取授权登陆二维码

    微信扫码登陆(1)—扫码登录流程讲解、获取授权登陆二维码扫码登录流程讲解、获取授权登陆二维码具体流程可以看微信官网的扫码登录文档地址:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN其实官方文档已…

    2022年4月26日
    100
  • centos7安装python 3.7_python安装后如何使用

    centos7安装python 3.7_python安装后如何使用文章目录什么是EPEL更新一下yum:安装EPEL:安装python3使用python3什么是EPELRHEL以及他的衍生发行版如CentOS、ScientificLinux为了稳定,官方的rpmrepository提供的rpm包往往是很滞后的,当然了,这样做这是无可厚非的,毕竟这是服务器版本,安全稳定是重点,官方的rpmrepository提供的rpm包也不够丰富,很多时候需要自己编译那太辛苦了,而EPEL恰恰可以解决这两方面的问题。什么是EPEL?EPEL的全称叫ExtraPackag

    2022年9月25日
    1
  • java加入md5_javamd5加密解密

    java加入md5_javamd5加密解密方法二、importjava.security.spec.*;classMD5_test{publicfinalstaticStringMD5(Strings){charhexDigits[]={‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’a’,’b’,’c’,’d’,’e’,’f’};try{byte…

    2022年7月14日
    34
  • ghost备份和还原_cgi备份还原

    ghost备份和还原_cgi备份还原制作系统镜像

    2022年9月5日
    2
  • TabNine的Pro激活码【中文破解版】

    (TabNine的Pro激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年4月2日
    63

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号