本文提供设备通过HTTP协议接入物联网平台的示例代码。

物联网平台支持设备通过HTTP协议接入。相关配置说明,请参见HTTP连接通信

本文提供基于Java HTTP的接入示例代码,介绍如何配置设备通过HTTP协议接入物联网平台的请求参数,计算设备端签名等。

说明 目前仅华东2(上海)地域支持HTTP接入。

pom.xml配置

pom.xml文件中,添加以下依赖,引入阿里fastjson包。

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.61</version>
</dependency>

示例代码

以下为设备通过HTTP协议接入物联网平台和消息通信的主体代码示例。

/*   
 * Copyright © 2019 Alibaba. All rights reserved.
 */
package com.aliyun.iot.demo;

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;

/**
 * 设备使用HTTP协议接入阿里云物联网平台 
 * 协议规范说明:https://help.aliyun.com/document_detail/58036.html
 * 数据格式:https://help.aliyun.com/document_detail/58034.html
 */
public class IotHttpClient {

    // 地域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";
        IotHttpClient client = new IotHttpClient();
        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 });
    }
}