阿里云消息队列的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% ...
随机推荐
- spring事务详解(一)初探事务
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 引子 很多 ...
- Freescale MKL16Z1288VF4 芯片调试接口
WDOG监视内部系统操作,并在发生故障时强制复位.它可以运行在一个独立的1 kHz低功率振荡器,具有可编程刷新窗口,以检测程序流或系统频率的偏差. 看门狗计时器保持一个时间在系统上运行,并重置它,以防 ...
- [蓝桥杯]PREV-21.历届试题_回文数字
问题描述 观察数字:, 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做:回文数字. 本题要求你找到一些5位或6位的十进制数字.满足如下要求: 该数字的各个数位之和等于输入 ...
- Ubuntu16.04修改IP及时生效
1.Network Connetions 窗口管理器中修改IP 2.ifconfig查看网卡名字 3.刷新IP sudo ip addr flush enp2s0 4.sudo service net ...
- 20175236 2018-2019-2 《Java程序设计》第五周学习总结
教材学习内容总结 接口回调 1.接口属于引用型变量,可以存放实现该接口类的实例的引用,即存放对象的引用. 2.接口回调理解上跟对象的上转型对象差不多. 理解接口 接口可以抽象出重要的行为标准. 接口多 ...
- python:面向对象初级
面向对象编程类的概念 : 具有相同属性和技能的一类事物 人类 抽象对象 : 就是对一个类的具体的描述 具体的人 具体 使用面向对象的好处: 使得代码之间的角色关系更加明确 增强了代码的可扩展性 规范了 ...
- word 添加文本框
转https://blog.csdn.net/sroco/article/details/17044973 如何在word2013(2007.2010)中添加带滚动条的文本框 2013年11月30日 ...
- ubuntu 装机步骤表
步骤 1. root 步骤 apt-get update ; apt-get upgrate apt-get install git zsh apt-get install -y make build ...
- SurfaceView绘图时刷新问题,尝试各种办法无法解决,请教高手
/** * */ 源码:http://pan.baidu.com/s/1i3FtdZZ 画图时最左面,第一帧总是出现一个黑条,其它的帧没有问题package com.macrosoft.testewa ...
- 【原创】Open JDK更换过程及更换后的问题总结与分析
由于2019年1月起Oracle对通用计算以外的应用场景开始收费,综合看来还是主要针对嵌入式的Java应用进行收费,毕竟嵌入式设备的数量是庞大的,可以有数亿元进账. 因Oracle JDK收费,各大公 ...