阿里云消息队列的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% ...
随机推荐
- [IDEA]IDEA设置注释模板
IDEA的注释模板有类注释模板和方法注释模板两种,下面分别介绍: 一.类注释模板 菜单路径:File->Settings->Editor->File and Code Templat ...
- TCP连接异常:broken pipe 和EOF
本文介绍3种TCP连接异常的情况. 1.server端没有启动,client尝试连接 ./client dial failed: dial tcp 127.0.0.1:8080: connect: c ...
- 第七届蓝桥杯省赛javaB组 第七题剪邮票
剪邮票 如[图1.jpg], 有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连)比如,[图2.jpg],[图3.jpg]中,粉红色所示部分就是合格的 ...
- [mybatis]Example的用法-转
转自:https://blog.csdn.net/zhemeban/article/details/71901759 Example类是什么? Example类指定如何构建一个动态的where子句. ...
- Ntrip协议简介(转)
原文地址:https://blog.csdn.net/sinat_19447667/article/details/67637167 1 什么是Ntrip? CORS(Continuously Ope ...
- form表单提交数据,页面必定会刷新,ajax提交数据不会刷新,做到悄悄提交,多选删除,ajax提交实例
很多页面用到的模态对话框,如知明网站https://dig.chouti.com/的登录页都是模态对话框, 当点登录时,是用的ajax提交,因为输入错了信息,有返回消息,而页面没有刷新. jquery ...
- Oracle中函数/过程返回多个值(结果集)
Oracle中函数/过程返回结果集的几种方式: 以函数return为例,存储过程只需改为out参数即可,在oracle 10g测试通过. (1) 返回游标: return的类型为:SYS_REFCUR ...
- pymsql简单的使用
不废话直接上代码: import pymysql class MysqlConnection: ''' 单例模式获取数据库链接实例 ''' _instance = None def __new__(c ...
- dubbo 在不同协议下携带上下文区别
如果走原生的dubbo协议,RpcContext.getContext()里的attarchments和values 是能够在节点间传递的 但如果hessian协议,attarchments和valu ...
- Windows系统配置
1.常用设置 (1)设置虚拟内存 右击:计算机-属性-高级系统设置-高级选项卡-性能组框设置按钮-高级选项卡-虚拟内存组框更改按钮-按需求设置 (2)设置休眠文件 改变休眠文件大小:powercfg ...