背景:目前就职于某家物联网公司,现根据官方文档调用有人云接口以做测试
A.取JSON(集成了hutool)
@Test void getToken() throws JSONException { JSONObject jsonObject = new JSONObject(); jsonObject.put("appKey", "*******"); jsonObject.put("appSecret", "*******"); // 添加请求头信息 Map<String, String> heads = new HashMap<>(); // 使用json发送请求,下面的是必须的 heads.put("Content-Type", "application/json;charset=UTF-8"); String url = "https://openapi.mp.usr.cn/usrCloud/user/getAuthToken"; HttpResponse response = HttpRequest.post(url) .headerMap(heads, false) .body(String.valueOf(jsonObject)) .timeout(5 * 60 * 1000) .execute(); System.out.println("接收有人云iot请求" + response); }
返回结果
接收有人云iot请求Response Headers:
Transfer-Encoding=[chunked]
null=[HTTP/1.1 200]
Server=[nginx]
Connection=[keep-alive]
Date=[Tue, 07 Mar 2023 08:27:48 GMT]
Content-Type=[application/json;charset=UTF-8]
Response Body:
{"status":0,"data":{"X-Access-Token":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxNTg4MDgzNTAwOGwiLCJhcHBLZXxxx0ZkJseVgzQSIsInVpZCI6NDc0MjMsImV4cCI6MTY3ODE4NDg2NywiaWF0IjoxNjc4MTc3NjY3fQ.aeLzrefDGMatrNiBrPgwdXYynClnJiLJtPfvz5GUTeo"},"info":"ok"}
B.请求工业接口数据
不演示了 在url\body\head正确的情况下都大同小异
最后附上有人云api链接
http://cloud.usr.cn/document/550.html
浙公网安备 33010602011771号