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. siftflow-fcn32s训练及预测

    一.说明 SIFT Flow 是一个标注的语义分割的数据集,有两个label,一个是语义分类(33类),另一个是场景标签(3类). Semantic and geometric segmentatio ...

  2. php5.5.7添加pgsql,pdo_pgsql,swoole

    一:下载php源码sudo wget cn2.php.net/distributions/php-X.X.X.tar.gz 二:解压 tar xzf /php-X.X.X.tar.gz 三:进入源码e ...

  3. version `GLIBC_2.17' not found 解决方法

    1.先查看是哪个函数用的是GLIBC_2.17 root@emb-pc:/home/emb/temp# nm lib61850.so | grep GLIBC_2.17 U clock_gettime ...

  4. mac中更改xampp的根目录

    方法一: 1 打开 应用程序->XAMPP->xamppfiles->etc->httpd.conf 文档 2 commond+f搜索htdocs,搜到如下结果 # Docum ...

  5. centos7卸载旧jdk安装新jdk1.8

    卸载旧JDK版本 需卸载centos7自带的JDK1.7 rpm -qa|grep jdk     列出已安装jdk版本 rpm -e --nodeps java-1.7.0-openjdk-1.7. ...

  6. 【原创】Open JDK更换过程及更换后的问题总结与分析

    由于2019年1月起Oracle对通用计算以外的应用场景开始收费,综合看来还是主要针对嵌入式的Java应用进行收费,毕竟嵌入式设备的数量是庞大的,可以有数亿元进账. 因Oracle JDK收费,各大公 ...

  7. Scrapy CrawlSpider源码分析

    crawl.py中主要包含两个类: 1. CrawlSpider 2. Rule link_extractor:传LinkExtractor实例对象 callback:传”func_name“ cb_ ...

  8. Jq写个联级菜单

    这个效果很好看,Jq很容易实现: $(document).ready(function(){ $('.menu li').hover(function(){ $(this).children('ul' ...

  9. 自己的mongodb的CRUD封装

    工具类:package Utils; import com.google.common.collect.Lists; import com.mongodb.MongoClient; import co ...

  10. 基础总结(02)--BFC(块级格式化上下文)

    BFC(块级格式化上下文)布局规则 1.元素垂直排列. 2.同一个BFC相邻两个元素的margin会重叠. 3.BFC区域不会与浮动元素重叠. 4.BFC就是页面上的一个隔离的独立容器,容器里面的子元 ...