大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/222978.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...