阿里云消息队列的C#使用http接口发送消息实例
app.config
<appSettings>
<clear/>
<add key="Ons_Topic" value="XXX_FinishOrder"/>
<add key="Ons_AccessKey" value="jmXXXXXBov"/>
<add key="Ons_SecretKey" value="VXXXXXjRD7pxYCpjtnJDDbsH"/>
<add key="Ons_ConsumerId" value="CID_xxxxxxxx"/>
<add key="Ons_ProducerID" value="PID_xxxxxxxxxxx"/>
</appSettings>
program.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using ons;
using test;
using System.Security.Cryptography; namespace MqSDk
{
class Program
{
/// <summary>
/// method to generate a MD5 hash of a string
/// </summary>
/// <param name="strToHash">string to hash</param>
/// <returns>hashed string</returns>
public static string GenerateMd5(string strToHash)
{
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] emailBytes = Encoding.UTF8.GetBytes(strToHash.ToLower());
byte[] hashedEmailBytes = md5.ComputeHash(emailBytes);
StringBuilder sb = new StringBuilder();
foreach (var b in hashedEmailBytes)
{
sb.Append(b.ToString("x2").ToLower());
}
return sb.ToString();
} static void Main(string[] args)
{
//# 公测url
string url = "http://publictest-rest.ons.aliyun.com/";
HttpClient client = new HttpClient();
string onsTopic = ConfigurationManager.AppSettings["Ons_Topic"];
string onsProducerId = ConfigurationManager.AppSettings["Ons_ProducerID"];
string onsAccessKey = ConfigurationManager.AppSettings["Ons_AccessKey"];
string onsSecretKey = ConfigurationManager.AppSettings["Ons_SecretKey"];
string onsConsumerId = ConfigurationManager.AppSettings["Ons_ConsumerId"];
String body = @"{""value"": ""test""}";
var newline = "\n";
for (int i = ; i < ; i++)
{
//var date = DateTime.Now.TimeOfDay.TotalMilliseconds.ToString("F0");
TimeSpan ts = DateTime.UtcNow - new DateTime(, , , , , , );
var date = Convert.ToInt64(ts.TotalMilliseconds).ToString();
HttpContent content = new StringContent(body);
var md5Str = GenerateMd5(body);
//db2421caefd6e8163e1928da4f53cc67
var signString = onsTopic + newline + onsProducerId + newline + md5Str + newline + date;
String sign = EncryptToSha1(signString, onsSecretKey);
Console.WriteLine(sign);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
content.Headers.Add("Signature", sign);
content.Headers.Add("AccessKey", onsAccessKey);
content.Headers.Add("ProducerID", onsProducerId);
var rurl = url + "message/?topic=" + onsTopic + "&time=" + date + "&tag=http" + "&key=http";
Console.WriteLine(rurl);
client.PostAsync(rurl, content).ContinueWith(
requestTask =>
{
// Get HTTP response from completed task.
HttpResponseMessage response = requestTask.Result; // Check that response was successful or throw exception
response.EnsureSuccessStatusCode(); // Read response asynchronously as JsonValue and write out top facts for each country
response.Content.ReadAsStringAsync().ContinueWith(
(readTask) =>
{
Console.WriteLine(readTask.Result);
});
}
);
}
Console.ReadLine();
} #region 获取由SHA1加密的字符串 /// <summary>
/// sha1 加密,与php加密结果一样
/// </summary>
/// <param name="str"></param>
/// <param name="keys"></param>
/// <returns></returns>
public static string EncryptToSha1(string str, string keys)
{
return hash_hmac(str, keys, true);
}
private static string hash_hmac(string signatureString, string secretKey, bool raw_output = false)
{
var enc = Encoding.UTF8;
HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(secretKey));
hmac.Initialize(); byte[] buffer = enc.GetBytes(signatureString);
if (raw_output)
{
return Convert.ToBase64String(hmac.ComputeHash(buffer));
}
else
{
return BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower();
}
}
#endregion }
}
阿里云消息队列的C#使用http接口发送消息实例的更多相关文章
- RabbitMQ入门教程(十七):消息队列的应用场景和常见的消息队列之间的比较
原文:RabbitMQ入门教程(十七):消息队列的应用场景和常见的消息队列之间的比较 分享一个朋友的人工智能教程.比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 这是网上的一篇教程写的很好,不知原作 ...
- springboot中activeMQ消息队列的引入与使用(发送短信)
1.引入pom依赖 <!--activemq--><dependency> <groupId>org.springframework.boot</groupI ...
- 消息队列入门(四)ActiveMQ的应用实例
>>部署和启动ActiveMQ 去官网下载:http://activemq.apache.org/ 我下载的是apache-activemq-5.12.0-bin.tar.gz, 解压到本 ...
- (转)RabbitMQ消息队列(九):Publisher的消息确认机制
在前面的文章中提到了queue和consumer之间的消息确认机制:通过设置ack.那么Publisher能不到知道他post的Message有没有到达queue,甚至更近一步,是否被某个Consum ...
- RabbitMQ消息队列安装和配置以及推送消息
好久没有写了,最近项目用到RabbitMQ,找了一些资料试验,最后终于成功了,把安装配置的步骤分享给大家. 一.Erlang安装具体过程: 1.双击otp_win32_R16801.exe(不同版本可 ...
- RabbitMQ消息队列(九):Publisher的消息确认机制
在前面的文章中提到了queue和consumer之间的消息确认机制:通过设置ack.那么Publisher能不到知道他post的Message有没有到达queue,甚至更近一步,是否被某个Consum ...
- 用过消息队列?Kafka?能否手写一个消息队列?懵
是否有同样的经历?面试官问你做过啥项目,我一顿胡侃,项目利用到了消息队列,kafka,rocketMQ等等. 好的,那请开始你的表演,面试官递过一支笔:给我手写一个消息队列!!WHAT? 为了大家遇到 ...
- 阿里云宣布进入 Serverless 容器时代,推出弹性容器实例服务 ECI
摘要: 阿里云宣布弹性容器实例 ECI(Elastic Container Instance)正式商业化. 为了应对业务高峰,打算提前多久执行ECS扩展?买了ECS虚拟机,容器规格不能完美装箱怎么办? ...
- 消息队列(七)--- RocketMQ延时发送和消息重试(半原创)
本文图片和部分总结来自于参考资料,半原创,侵删 问题 Rocketmq 重试是否有超时问题,假如超时了如何解决,是重新发送消息呢?还是一直等待 假如某个 msg 进入了重试队列(%RETRY_XXX% ...
随机推荐
- Btrace介绍
一.Btrace简介 BTrace可以动态的向目标应用程序的字节码注入追踪代码 用到的技术JavaComplierApi,JVMTI,Agent,Instrumentation+ASM 二.Btrac ...
- jq源码判断数据类型
4.Object.prototype.toString.call() 1 var a = Object.prototype.toString; 2 3 console.log(a.call(" ...
- 【Angular】——TypeScript之胖箭头(=>)函数
前言:胖箭头(=>)函数是一种快速书写函数的简介语法. ES5和TypeScript比较:在ES5中,每当我们要用甘薯作为方法参数时,都必须用function关键字和紧随其后的花括号({})表示 ...
- CentOS7下安装Python3并保留Python2
1. 安装make编译指令的依赖环境 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel ...
- L1、L2范数理解
读了博主https://blog.csdn.net/a493823882/article/details/80569888的文章做简要的记录. 范数可以当作距离来理解. L1范数: 曼哈顿距离,是机器 ...
- Zuul转发请求时HttpHostConnectException can't cast to ZuulException问题解决方法
看了一下github上的issue,这应该是一个bug.说是已经在zuul 2.0.1.RELEASE中处理了,但是我用的SpringBoot2.0.4.RELEASE中仍然有问题. 处理方案如下: ...
- go语言学习--string、int、int64互相转换,字符串的截取,数组和字符串的转换
下面总结了go中常用的转换 #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt ...
- ubuntu16.04 install qtcreator
1. 安装相关软件,搭建环境 sudo apt install qt-creator sudo apt install qt5-default source python35/bin/activate ...
- AD服务无法启动
转自网络资源:http://www.great-one.co.uk/archives/289 版本:win08 : 该方法支持hyper-v 虚拟机 启动报错: A Windows 2008 R2 D ...
- 转: 解决Setting property 'source' to 'org.eclipse.jst.jee.server的问题
我发现这个问题上网搜索 ,找到的地址为:http://blog.csdn.net/z69183787/article/details/19911935 .但是他的标题上也有一个"转" ...