java实现emqtt的消息发布
来源:互联网 发布:程序员薪资城市排行榜 编辑:程序博客网 时间:2024/06/06 15:39
package com.hthl.xxtd.sdk.push.emqtt;import javax.resource.spi.CommException;import org.apache.commons.codec.binary.Base64;import org.apache.commons.httpclient.Header;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.log4j.Logger;import com.hthl.xxtd.util.DateUtil;/** * EMQ客户端工具 * */public class EmqHttpUtil {private static final Logger logger = Logger.getLogger(EmqHttpUtil.class);public static void main(String[] args) throws Exception {NameValuePair[] data={new NameValuePair("qos", "1"),new NameValuePair("retain", "0"),new NameValuePair("topic", "hthl/hyt"),new NameValuePair("message", "大吉大利,今晚吃鸡:"+DateUtil.getTimes())};System.out.println(httpPost("http://192.168.20.38:6060/mqtt/publish", data, "UTF-8",getHeader()));}/** * 构造Basic Auth认证头信息 * @throws Exception */private static String getHeader() throws Exception { String auth = "hthl_sub" + ":" + "123456";byte[] encodedAuth=Base64.encodeBase64(auth.getBytes("UTF-8"));String authHeader = "Basic " +new String(encodedAuth);return authHeader;}/** * HTTP/HTTPS POST * @param url 接口地址 * @param data 请求数据 * @param encoding 编码方式 * @return String * @throws CommException */public static String httpPost(String url, NameValuePair[] data,String encoding,String authHeader)throws Exception {HttpClient httpClient = new HttpClient();PostMethod postMethod = new PostMethod(url);// 设置编码,http post同时会用编码进行url.encodehttpClient.getParams().setContentCharset(encoding); postMethod.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset="+encoding); postMethod.addRequestHeader("Authorization", authHeader);// 将表单的值放入postMethod中postMethod.setRequestBody(data);// 执行postMethodint statusCode =httpClient.executeMethod(postMethod); logger.info("Response status code: " + statusCode);//返回200为成功// HttpClient对于要求接受后继服务的请求,POST和PUT等不能自动处理转发// 301或者302if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {// 从头中取出转向的地址Header locationHeader = postMethod.getResponseHeader("location");String location = null;if (locationHeader != null) {location = locationHeader.getValue();logger.error("The page was redirected to:" + location);} else {logger.error("Location field value is null.");}return null;} else {return postMethod.getResponseBodyAsString();}}}
阅读全文
1 0
- java实现emqtt的消息发布
- Java实现Redis的消息订阅和发布
- Java实现Redis的消息订阅和发布
- Java实现Redis的消息订阅和发布
- Java实现redis的消息订阅和发布
- Java实现Redis的消息订阅和发布
- Java实现Redis的消息订阅和发布
- 关于emqtt使用的一些心得
- Python下emqtt服务的简单搭建
- MQTT fusesource 的 Callback API 消息发布的实现
- emqtt中auth-mysql插件与emqtt服务器连接的配置
- 消息的发布
- Blazeds 与FLEX的发布问题 消息实现
- 使用Spring-Redis实现消息的发布/订阅
- Java基于IBMMQ消息的发布与订阅
- jedis实现redis的消息队列、发布对象消息、字节数组与字符串相互转换
- 【Redis】jedis客户端实现redis消息的发布订阅(实时消息中间件)
- jedis实现redis的消息队列、发布对象消息、字节数组与字符串相互转换
- 去除WinRAR的广告
- 大数据正式20
- 经典算法---堆排序(Heapsort)
- IntelliJ IDEA (2017.3)激活
- Django中Mixin和View组合技巧
- java实现emqtt的消息发布
- httpclient
- HDOJ1405 The Last Practice(有坑)
- 2. 什么是对象
- G盘显示拒绝访问要如何办啊
- react-native-swiper 让人无语的坑
- 虚拟网卡 tun/tap口创建及介绍
- 郁闷的出纳员
- the mathtype dll cannot be found word2010(word2013类似)