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接口发送消息实例的更多相关文章

  1. RabbitMQ入门教程(十七):消息队列的应用场景和常见的消息队列之间的比较

    原文:RabbitMQ入门教程(十七):消息队列的应用场景和常见的消息队列之间的比较 分享一个朋友的人工智能教程.比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 这是网上的一篇教程写的很好,不知原作 ...

  2. springboot中activeMQ消息队列的引入与使用(发送短信)

    1.引入pom依赖 <!--activemq--><dependency> <groupId>org.springframework.boot</groupI ...

  3. 消息队列入门(四)ActiveMQ的应用实例

    >>部署和启动ActiveMQ 去官网下载:http://activemq.apache.org/ 我下载的是apache-activemq-5.12.0-bin.tar.gz, 解压到本 ...

  4. (转)RabbitMQ消息队列(九):Publisher的消息确认机制

    在前面的文章中提到了queue和consumer之间的消息确认机制:通过设置ack.那么Publisher能不到知道他post的Message有没有到达queue,甚至更近一步,是否被某个Consum ...

  5. RabbitMQ消息队列安装和配置以及推送消息

    好久没有写了,最近项目用到RabbitMQ,找了一些资料试验,最后终于成功了,把安装配置的步骤分享给大家. 一.Erlang安装具体过程: 1.双击otp_win32_R16801.exe(不同版本可 ...

  6. RabbitMQ消息队列(九):Publisher的消息确认机制

    在前面的文章中提到了queue和consumer之间的消息确认机制:通过设置ack.那么Publisher能不到知道他post的Message有没有到达queue,甚至更近一步,是否被某个Consum ...

  7. 用过消息队列?Kafka?能否手写一个消息队列?懵

    是否有同样的经历?面试官问你做过啥项目,我一顿胡侃,项目利用到了消息队列,kafka,rocketMQ等等. 好的,那请开始你的表演,面试官递过一支笔:给我手写一个消息队列!!WHAT? 为了大家遇到 ...

  8. 阿里云宣布进入 Serverless 容器时代,推出弹性容器实例服务 ECI

    摘要: 阿里云宣布弹性容器实例 ECI(Elastic Container Instance)正式商业化. 为了应对业务高峰,打算提前多久执行ECS扩展?买了ECS虚拟机,容器规格不能完美装箱怎么办? ...

  9. 消息队列(七)--- RocketMQ延时发送和消息重试(半原创)

    本文图片和部分总结来自于参考资料,半原创,侵删 问题 Rocketmq 重试是否有超时问题,假如超时了如何解决,是重新发送消息呢?还是一直等待 假如某个 msg 进入了重试队列(%RETRY_XXX% ...

随机推荐

  1. delphi 按键测试

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  2. Hello 2019

    2019也要创造价值! 热爱生活!

  3. Spring的IOC原理

    1. IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机 ...

  4. Python高级变量类型

    1.列表基础操作 2.列表循环遍历 3.元组 4.字典基础操作 5.字典循环遍历 6.字符串基础操作 7.字符串切片 8.公共方法

  5. 2018-2019-2 20175227张雪莹《Java程序设计》实验三 《敏捷开发与XP实践》

    2018-2019-2 20175227张雪莹<Java程序设计> 实验三 <敏捷开发与XP实践> 实验报告封面 课程:Java程序设计 班级:1752班 姓名:张雪莹 学号: ...

  6. java大数据量调优

    从总体上来看,对于大型网站,比如门户网站,在面对大量用户访问.高并发请求方面,基本的解决方案集中在这样几个环节:1.首先需要解决网络带宽和Web请求的高并发,需要合理的加大服务器和带宽的投入,并且需要 ...

  7. 'pip' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    之前python2.7是没有pip的,需要安装python3.6 以上. 但是安装3.6版本后,还是一样.官网提示需要执行以下命令:python -m ensurepip --default-pip ...

  8. Python【每日一问】11

    问: 请简单说明一下以下代码的执行过程以及最终的输出结果 def fib(max): n, a, b = 0, 0, 1 while n < max: yield b a, b = b, a + ...

  9. Web API的接口访问安全性

    使用签名获取Token 首先我们自定义appkey.appSecret.可用GUID随机生成,AppSecret要不定期更换.然后放到配置文件中. Appkey=1AF62C68-B970-46E7- ...

  10. Android中的透明度

    最近在实践中,在一个设置了背景图的LinearLayout中放一个透明度15%,背景色为#ffffff的容器,里面再放白色#ffffff的文本,开始容器用background="#fffff ...