RabbitMQ提供了HTTP API手册,发现其中有获取队列情况的API。(本地的API手册地址为:http://localhost:15672/api)
所有API调用都需要做权限验证,需在请求头部中加入权限验证信息
1.获取所有队列信息
http://host:15672/api/queues
2.获取单个队列信息
http://host:15672/api/queues/vhost/name
host为RabbitMQ部署地址,vhost为队列所在的虚拟主机名,name为队列名。
注意:若队列所在默认虚拟主机,即主机名为"/",请求时需将"/"url转码后("%2f")请求
static void Main(string[] args) { #region MyRegion //通过RabbitMq的Api来获取相关队列的信息 var url = " http://192.168.0.37:15672/api/queues/%2f/Citms.Queue.LXEPP.VideoMq"; //创建HttpClient(注意传入HttpClientHandler) var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; var username = "admin"; var password = "citms"; using (var http = new HttpClient(handler)) { string auth = username + ":" + password; byte[] b = System.Text.Encoding.Default.GetBytes(auth); var a = Convert.ToBase64String(b); http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", a); var response = http.GetAsync(url).Result;//传参使用 //确保HTTP成功状态值 response.EnsureSuccessStatusCode(); //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip) var result = response.Content.ReadAsStringAsync().Result; var json = JsonConvert.DeserializeObject<dynamic>(result); } }