import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import com.alibaba.fastjson.JSONObject;
import com.abcd.entity.DevicePermissionDTO;
import com.abcd.entity.ScenePermissionDTO;
import com.abcd.util.Base64Utils;
import sun.misc.BASE64Encoder;
public class HttpClientTest2 {
final BASE64Encoder encoder = new BASE64Encoder();
@Test
public void doGet()throws Exception{
// 创建一个httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个GET对象
HttpGet get = new HttpGet("http://www.sogou.com");
// 执行请求
CloseableHttpResponse response = httpClient.execute(get);
// 取响应的结果
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity,"utf-8");
System.out.println(string);
// 关闭httpClient
response.close();
httpClient.close();
}
@Test
public void doGetWithParam()throws Exception {
// 创建一个httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个uri对象
URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");
uriBuilder.addParameter("query","花千骨");
HttpGet get = new HttpGet(uriBuilder.build());
// 执行请求
CloseableHttpResponse response = httpClient.execute(get);
// 取响应的结果
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity,"utf-8");
System.out.println(string);
// 关闭httpClient
response.close();
httpClient.close();
}
@Test
public void doPost()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/httpClient/post");
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void doPostWithParam()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/httpClient/post");
String apiKey = " 8da327bfa92d49e3854a43c097422945";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
List<NameValuePair> kvList = new ArrayList<NameValuePair>();
kvList.add(new BasicNameValuePair("username", "zhangsan"));
kvList.add(new BasicNameValuePair("password", "1234"));
// 包装成一个Entity对象
StringEntity entity = new UrlEncodedFormEntity(kvList,"utf-8");
post.setEntity(entity);
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void register()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/register");
post.addHeader("Content-Type", "application/json");
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("name", "leon");
obj.put("email", "Leon.Sun@qq.com");
obj.put("password", "81dc9bdb52d04dc20036dbd8313ed055");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void login()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/login");
post.addHeader("Content-Type", "application/json");
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("email", "Leon.Sun@qq.com");
obj.put("password", "81dc9bdb52d04dc20036dbd8313ed055");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void reset()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/reset");
post.addHeader("Content-Type", "application/json");
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("email", "Leon.Sun@qq.com");
obj.put("password", "b59c67bf196a4758191e42f76670ceba");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void verify()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/verify");
post.addHeader("Content-Type", "application/json");
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("email", "Leon.Sun@qq.com");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void addHome()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/homeManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "b0626c1844d04a0f84098429e7b76206";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
String homePic = Base64Utils.ImageToBase64ByOnline("http://img.mp.itc.cn/upload/20161102/39a9c53cdc9a45aa9e0416cfef472ba8_th.jpg");
obj.put("homeName", "HM001");
obj.put("homePic", homePic);
obj.put("userId", "f9279a2b04ea44be954ce0586889bf93");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void updateHome()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
//HttpPost post = new HttpPost("http://localhost:8080/test/homeManage");
HttpPut post = new HttpPut("http://localhost:8080/test/homeManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "b0626c1844d04a0f84098429e7b76206";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
String homePic = Base64Utils.ImageToBase64ByOnline("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1540459468&di=7207831fbbfe4411e3368b0cbdf58836&imgtype=jpg&er=1&src=http%3A%2F%2Ffile29.mafengwo.net%2FM00%2F1A%2F82%2FwKgBpVXqF1aAKjiPAAJ5OmoOpmY53.rbook_comment.w1024.jpeg");
obj.put("homeId", "HM57d2a228a9aa4ad68fdbfc4d0c78dd17");
obj.put("homeName", "HM005");
obj.put("homePic", homePic);
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void deleteHome()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
//HttpDelete post = new HttpDelete("http://localhost:8080/test/homeManage");
HttpDeleteWithBody post = new HttpDeleteWithBody("http://localhost:8080/test/homeManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "b0626c1844d04a0f84098429e7b76206";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("homeId", "HM40fffa5c4ca44d48a21818fb8e52a02b");
obj.put("userId", "f9279a2b04ea44be954ce0586889bf93");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void addUserHome()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/homeManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "3af8b71ed93a416fa87789f3dc52f4bb";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("homeId", "HM40fffa5c4ca44d48a21818fb8e52a02b");
obj.put("userId", "67529fc0648a47b0806973448155793e");
obj.put("role", 0);
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void updateRoleByHomeIdAndUserId()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
//HttpPost post = new HttpPost("http://localhost:8080/test/homeManage");
HttpPut post = new HttpPut("http://localhost:8080/test/homeManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "b0626c1844d04a0f84098429e7b76206";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("homeId", "HMb7fca19a85c949f5b0913c350fe9cd13");
obj.put("userId", "f9279a2b04ea44be954ce0586889bf93");
obj.put("role", 1);
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void deleteUserHomeByHomeIdAndUserId()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
//HttpDelete post = new HttpDelete("http://localhost:8080/test/homeManage");
HttpDeleteWithBody post = new HttpDeleteWithBody("http://localhost:8080/test/homeManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "b21889d3b3bc4e278c61912db461b146";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("homeId", "HMb7fca19a85c949f5b0913c350fe9cd13");
obj.put("userId", "bae55ece7ff44e7c993431f4a2a3652f");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void addRoom()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/roomManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "b0626c1844d04a0f84098429e7b76206";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
String roomPic = Base64Utils.ImageToBase64ByOnline("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3136587119,1278524658&fm=26&gp=0.jpg");
obj.put("roomName", "RM001");
obj.put("floorName", "FL001");
obj.put("userId", "f9279a2b04ea44be954ce0586889bf93");
obj.put("homeId", "HM40fffa5c4ca44d48a21818fb8e52a02b");
obj.put("roomPic", roomPic);
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void updateRoomByRoomId()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
//HttpPost post = new HttpPost("http://localhost:8080/test/homeManage");
HttpPut post = new HttpPut("http://localhost:8080/test/roomManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "b0626c1844d04a0f84098429e7b76206";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
String roomPic = Base64Utils.ImageToBase64ByOnline("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1539922771019&di=fb9ed17dec00966c92bc4a450fc4653e&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F017d145a3db5eda80121974198b99e.jpg%401280w_1l_2o_100sh.jpg");
obj.put("roomPic", roomPic);
obj.put("roomId", "RM6256753957654ff3a7da251218a65778");
obj.put("roomName", "RM002");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void deleteRoom()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
//HttpDelete post = new HttpDelete("http://localhost:8080/test/homeManage");
HttpDeleteWithBody post = new HttpDeleteWithBody("http://localhost:8080/test/roomManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "6a1ef015e13b481cbb600d60acd41187";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("roomId", "RM113bdece068741f092cd1df5c8208719");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void addDevice()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/deviceManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "b0626c1844d04a0f84098429e7b76206";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("deviceName", "DV001");
obj.put("roomId", "RM1fd425d52f5241c19c680172c26abd15");
obj.put("userId", "f9279a2b04ea44be954ce0586889bf93");
obj.put("deviceModel", "DM001");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void updateDeviceByDeviceId()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
//HttpPost post = new HttpPost("http://localhost:8080/test/homeManage");
HttpPut post = new HttpPut("http://localhost:8080/test/deviceManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "b0626c1844d04a0f84098429e7b76206";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("deviceId", "DV1ee76a261b994b4c9ad9892a320aac6b");
obj.put("deviceName", "DV002");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void changeDeviceLocation()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
//HttpPost post = new HttpPost("http://localhost:8080/test/homeManage");
HttpPut post = new HttpPut("http://localhost:8080/test/deviceManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "b0626c1844d04a0f84098429e7b76206";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("deviceId", "DV1ee76a261b994b4c9ad9892a320aac6b");
obj.put("roomId", "RM75b207fb7b334536b9781266d8238b82");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void deleteDevice()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
//HttpDelete post = new HttpDelete("http://localhost:8080/test/homeManage");
HttpDeleteWithBody post = new HttpDeleteWithBody("http://localhost:8080/test/deviceManage");
post.addHeader("Content-Type", "application/json");
String apiKey = "b0626c1844d04a0f84098429e7b76206";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("deviceId", "DV1ee76a261b994b4c9ad9892a320aac6b");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void dashboard()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/dashboard");
post.addHeader("Content-Type", "application/json");
String apiKey = "b21889d3b3bc4e278c61912db461b146";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("userId", "c662e68e504745b4832b9dbb4dbbe54b");
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void assignRoom()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/permissionManagement");
post.addHeader("Content-Type", "application/json");
String apiKey = "3af8b71ed93a416fa87789f3dc52f4bb";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("userId", "67529fc0648a47b0806973448155793e");
List<RoomPermissionDTO> list = new ArrayList<RoomPermissionDTO>();
RoomPermissionDTO room1 = new RoomPermissionDTO();
room1.setRoomId("RM1fd425d52f5241c19c680172c26abd15");
room1.setPermission(0);
RoomPermissionDTO room2 = new RoomPermissionDTO();
room2.setRoomId("RM5cd143c87c2149ea9f7a1c06889d3ab5");
room2.setPermission(1);
list.add(room1);
list.add(room2);
obj.put("roomPermission", list);
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void assignDevice()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/permissionManagement");
post.addHeader("Content-Type", "application/json");
String apiKey = "3af8b71ed93a416fa87789f3dc52f4bb";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("userId", "67529fc0648a47b0806973448155793e");
List<DevicePermissionDTO> list = new ArrayList<DevicePermissionDTO>();
DevicePermissionDTO device1 = new DevicePermissionDTO();
device1.setDeviceId("DV588587cbe2ad4c7d82da2ab1cc64f451");
device1.setPermission(0);
DevicePermissionDTO device2 = new DevicePermissionDTO();
device2.setDeviceId("DV8a7f3350579c459eb3b4236ba6cf9188");
device2.setPermission(1);
list.add(device1);
list.add(device2);
obj.put("devicePermission", list);
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void assignScene()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/permissionManagement");
post.addHeader("Content-Type", "application/json");
String apiKey = "3af8b71ed93a416fa87789f3dc52f4bb";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("userId", "67529fc0648a47b0806973448155793e");
List<ScenePermissionDTO> list = new ArrayList<ScenePermissionDTO>();
ScenePermissionDTO scene1 = new ScenePermissionDTO();
scene1.setSceneId("b4a551b397404780ae57a6132e4438e4");
scene1.setPermission(0);
ScenePermissionDTO scene2 = new ScenePermissionDTO();
scene2.setSceneId("0c2c9f7c72834557a3010d65742d39c5");
scene2.setPermission(1);
list.add(scene1);
list.add(scene2);
obj.put("scenePermission", list);
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void addDeviceInfo()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/addDevice");
post.addHeader("Content-Type", "application/json");
String apiKey = "b21889d3b3bc4e278c61912db461b146";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("deviceId", "6e811724a00b400d8807346305390340");
obj.put("deviceName", "DV006");
obj.put("parentId", "081a068d8b394795917e27e9f81c90c7");
obj.put("mac", "xxxxxxxxxxxxxx99");
obj.put("roomId", "RMc662e68e504745b4832b9dbb4dbbe54b");
obj.put("deviceModel", 5);
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
@Test
public void updateDeviceInfo()throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/test/updateDeviceInfo");
post.addHeader("Content-Type", "application/json");
String apiKey = "b21889d3b3bc4e278c61912db461b146";
post.addHeader("Authorization", apiKey);
// 设置请求的内容
// 创建一个Entity.模拟一个表单
JSONObject obj = new JSONObject();
obj.put("deviceId", "c33583ae3ee0419eb872cb7ec2622bc7");
obj.put("deviceName", "DV001");
obj.put("parentId", "0a325301e71c45a59d50ac5f0d4a2ca9");
//obj.put("parentId", "");
obj.put("mac", "xxxxxxxxxxxxxxxx");
obj.put("roomId", "RMc662e68e504745b4832b9dbb4dbbe54b");
obj.put("deviceModel", 2);
post.setEntity(new StringEntity(obj.toString()));
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
}