本文主要是写一个java的http客户端,使用http连接至阿里云的物联网平台。主要参考阿里云的官方开发文档:阿里云物联网平台官方文档
目录
一、简介
阿里云的物联网平台主要是采用Topic方式来进行云端和设备的通信。阿里云官方比较支持采用MQTT的方式来通信。当然,目前在国内的设备是支持以http方式进行通信的。使用Java开发一个http客户端并不难,只不过在这里需要添加阿里云的Maven库中json数据格式的支持,后面会具体说到。
二、开发平台
Eclipse、阿里云物联网平台
三、开发限制
- HTTP通信方式只适合单纯的数据上报方式,不支持事件上报等物模型编程方式
- 只有国内的设备才支持HTTP通信
- 只支持HTTPS协议
- HTTP请求方式只支持POST
- 数据上行接口传输数据大小限制为128KB
四、开发流程
1.云端开发
1.1注册阿里云物联网平台
注册并登录阿里云物联网平台:链接
进入物联网平台之后,创建实例,可以选择免费的版本的,然后进入物联网平台的控制台。
1.2新建产品和设备
点击 设备管理——产品——新建产品,创建自己的产品,根据自己的情况输入内容,品类建议选择为自定义品类,认证方式和数据格式默认就行。
创建完产品就可以新建设备了,其中DeviceName很重要,尽量用自己容易记住的,备注名称可以不写。
创建完设备,我们可以获取到设备的三元组信息了。三元组就是ProductKey、DeviceName、DeviceSecret,用于认证产品的。如果是动态注册的产品还要用到ProductSecret。三元组信息我们后面的客户端要用到。
1.3设置Topic
创建完设备和产品之后,进入产品详情,单击Topic列表,将我们所要通信的Topic设置权限为发布和订阅。
2.在Eclipse上实现HTTP客户端
2.1新建工程
接下来回到我们本地进行开发,首先在Eclipse平台创建工程,可以直接在创建时选择支持Maven的工程,也可以创建之后再添加为Maven Project。
创建之后我们就会看到有一个pom.xml文件,就是用于管理Maven库的。
然后在src中添加一个java文件,用于放置我们运行的代码。
2.2添加依赖
打开pom.xml文件,可以点击下面的Dependencies窗口,点击add输入groupId、artifactId、version的信息(填写的内容参照下面的代码),也可以切换到pom.xml文件界面,添加下面这段代码。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
此处注意官方文档中的version为1.2.61,但是我在实际操作中发现1.2.61版本并没有所需要的com.alibaba.fastjson.JSONObject的包。通过maven访问了该代码的GitHub,发现其最新版本为1.2.68,于是我换成了最新的版本就解决了。
2.3实现设备认证
设备认证需要使用设备端签名,签名生成采用hmac加密算法。
具体的加密规则见阿里云的开发文档。
private String sign(JSONObject params, String deviceSecret) {
// 请求参数按字典顺序排序
Set<String> keys = getSortedKeys(params);
// sign,signmethod 和 version除外
keys.remove("sign");
keys.remove("signmethod");
keys.remove("version");
// 组装签名明文
StringBuffer content = new StringBuffer();
for (String key : keys) {
content.append(key);
content.append(params.getString(key));
}
// 计算签名
String sign = encrypt(content.toString(), deviceSecret);
System.out.println("sign content=" + content);
System.out.println("sign result=" + sign);
return sign;
}
然后将设备的信息生成HTTP报文主体。
private String authBody(String productKey, String deviceName, String deviceSecret) {
// 构建认证请求
JSONObject body = new JSONObject();
body.put("productKey", productKey);
body.put("deviceName", deviceName);
body.put("clientId", productKey + "." + deviceName);
body.put("timestamp", String.valueOf(System.currentTimeMillis()));
body.put("signmethod", HMAC_ALGORITHM);
body.put("version", "default");
body.put("sign", sign(body, deviceSecret));
System.out.println("----- auth body -----");
System.out.println(body.toJSONString());
return body.toJSONString();
}
最后实现connenct连接。
public void conenct(String productKey, String deviceName, String deviceSecret) {
try {
// 注册地址
URL url = new URL("https://iot-as-http." + regionId + ".aliyuncs.com/auth");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(authBody(productKey, deviceName, deviceSecret));
// flush输出流的缓冲
out.flush();
// 获取URLConnection对象对应的输入流
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// 读取URL的响应
String result = "";
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println("----- auth result -----");
System.out.println(result);
// 关闭输入输出流
in.close();
out.close();
conn.disconnect();
// 获取token
JSONObject json = JSONObject.parseObject(result);
if (json.getIntValue("code") == 0) {
token = json.getJSONObject("info").getString("token");
}
} catch (Exception e) {
e.printStackTrace();
}
}
2.4实现数据上报
数据上报实现与设备认证类似,只是http的报文头有所修改,要添加入Topic的信息。
public void publish(String topic, byte[] payload) {
try {
// 注册地址
URL url = new URL("https://iot-as-http." + regionId + ".aliyuncs.com/topic" + topic);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", "application/octet-stream");
conn.setRequestProperty("password", token);
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream());
out.write(payload);
out.flush();
// 获取URLConnection对象对应的输入流
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// 读取URL的响应
String result = "";
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println("----- publish result -----");
System.out.println(result);
// 关闭输入输出流
in.close();
out.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
3.运行设备认证方法获取token
4.运行设备数据上报方法
五、完整代码
package main;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import com.alibaba.fastjson.JSONObject;
public class AliyunHttp {
// 地域ID,当前仅支持华东2(上海)
private static String regionId = "cn-shanghai";
// 定义加密方式。MAC算法可选以下多种算法 HmacMD5 HmacSHA1,需和signmethod一致
private static final String HMAC_ALGORITHM = "hmacsha1";
// token 7天有效,失效后需要重新获取
private String token = null;
/**
* 初始化HTTP客户端
*
* @param productKey 产品key
* @param deviceName 设备名称
* @param deviceSecret 设备密钥
*/
public void conenct(String productKey, String deviceName, String deviceSecret) {
try {
// 注册地址
URL url = new URL("https://iot-as-http." + regionId + ".aliyuncs.com/auth");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(authBody(productKey, deviceName, deviceSecret));
// flush输出流的缓冲
out.flush();
// 获取URLConnection对象对应的输入流
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// 读取URL的响应
String result = "";
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println("----- auth result -----");
System.out.println(result);
// 关闭输入输出流
in.close();
out.close();
conn.disconnect();
// 获取token
JSONObject json = JSONObject.parseObject(result);
if (json.getIntValue("code") == 0) {
token = json.getJSONObject("info").getString("token");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送消息
*
* @param topic 发送消息的Topic
* @param payload 消息内容
*/
public void publish(String topic, byte[] payload) {
try {
// 注册地址
URL url = new URL("https://iot-as-http." + regionId + ".aliyuncs.com/topic" + topic);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", "application/octet-stream");
conn.setRequestProperty("password", token);
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream());
out.write(payload);
out.flush();
// 获取URLConnection对象对应的输入流
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// 读取URL的响应
String result = "";
String line = "";
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println("----- publish result -----");
System.out.println(result);
// 关闭输入输出流
in.close();
out.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成认证请求内容
*
* @param params 认证参数
* @return 认证请求消息体
*/
private String authBody(String productKey, String deviceName, String deviceSecret) {
// 构建认证请求
JSONObject body = new JSONObject();
body.put("productKey", productKey);
body.put("deviceName", deviceName);
body.put("clientId", productKey + "." + deviceName);
body.put("timestamp", String.valueOf(System.currentTimeMillis()));
body.put("signmethod", HMAC_ALGORITHM);
body.put("version", "default");
body.put("sign", sign(body, deviceSecret));
System.out.println("----- auth body -----");
System.out.println(body.toJSONString());
return body.toJSONString();
}
/**
* 设备端签名
*
* @param params 签名参数
* @param deviceSecret 设备密钥
* @return 签名十六进制字符串
*/
private String sign(JSONObject params, String deviceSecret) {
// 请求参数按字典顺序排序
Set<String> keys = getSortedKeys(params);
// sign,signmethod 和 version除外
keys.remove("sign");
keys.remove("signmethod");
keys.remove("version");
// 组装签名明文
StringBuffer content = new StringBuffer();
for (String key : keys) {
content.append(key);
content.append(params.getString(key));
}
// 计算签名
String sign = encrypt(content.toString(), deviceSecret);
System.out.println("sign content=" + content);
System.out.println("sign result=" + sign);
return sign;
}
/**
* 获取JSON对象排序后的key集合
*
* @param json 需要排序的JSON对象
* @return 排序后的key集合
*/
private Set<String> getSortedKeys(JSONObject json) {
SortedMap<String, String> map = new TreeMap<String, String>();
for (String key : json.keySet()) {
String vlaue = json.getString(key);
map.put(key, vlaue);
}
return map.keySet();
}
/**
* 使用 HMAC_ALGORITHM 加密
*
* @param content 明文
* @param secret 密钥
* @return 密文
*/
private String encrypt(String content, String secret) {
try {
byte[] text = content.getBytes(StandardCharsets.UTF_8);
byte[] key = secret.getBytes(StandardCharsets.UTF_8);
SecretKeySpec secretKey = new SecretKeySpec(key, HMAC_ALGORITHM);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return byte2hex(mac.doFinal(text));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 二进制转十六进制字符串
*
* @param b 二进制数组
* @return 十六进制字符串
*/
private String byte2hex(byte[] b) {
StringBuffer sb = new StringBuffer();
for (int n = 0; b != null && n < b.length; n++) {
String stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1) {
sb.append('0');
}
sb.append(stmp);
}
return sb.toString().toUpperCase();
}
public static void main(String[] args) {
String productKey = "此处填写productkey";
String deviceName = "此处填写devicename";
String deviceSecret = "此处填写devicesecret";
AliyunHttp client = new AliyunHttp();
client.conenct(productKey, deviceName, deviceSecret);
// 发送消息的Topic。可在控制台自定义,设备有发布权限
String updateTopic = "/" + productKey + "/" + deviceName + "/user/update";
client.publish(updateTopic, "hello http".getBytes(StandardCharsets.UTF_8));
client.publish(updateTopic, new byte[] { 0x01, 0x02, 0x03 });
}
}