最近在做一个定制需求,有一个人脸设备要嵌入到平台,让平台支持下发人脸信息和记录的落库数据
1.第一次做这种对接,提供了一个API,后来才明白了我们对接设备要提供两个服务;
ServerApi代表是设备的那一端,我们要给设备返回过去他要的数据,具体的接口之类的就不展示了;提供一个思路
2.DevApi
服务端,是向设备推送的服务接口
需要提供http发送服务,也就是本文章的核心表达意思;POST发送请求
JSONObject jsonObject = new JSONObject();
jsonObject.put("DeviceUniqueCode",personMessageVos.getDeviceUniqueCode());
jsonObject.put("TimeStamp", DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
// jsonObject 发送的参数数据 PersonProgramCommon.POST_HTTP: http:// personMessageVos.getDeviceIp()//设备IP PersonProgramCommon.POST_DEVICEPOST//端口号 PersonProgramCommon.DEVICE_SERVICE_METOD_DOWNLOADAUTHORITYDATA//远程方法名
String postData = HttpRequestUtil.post(jsonObject, PersonProgramCommon.POST_HTTP+personMessageVos.getDeviceIp()+":"+PersonProgramCommon.POST_DEVICEPOST+PersonProgramCommon.DEVICE_SERVICE_METOD_DOWNLOADAUTHORITYDATA);
TdLog.info("添加人脸数据返回",postData);
发送HttpClients端接口工具类
public static String post(JSONObject json, String url){
String result = "";
HttpPost post = new HttpPost(url);
try{
CloseableHttpClient httpClient = HttpClients.createDefault();
post.setHeader("Content-Type","application/json;charset=utf-8");
post.addHeader("Authorization", "Basic YWRtaW46");
StringEntity postingString = new StringEntity(json.toString(),"utf-8");
post.setEntity(postingString);
HttpResponse response = httpClient.execute(post);
InputStream in = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
StringBuilder strber= new StringBuilder();
String line = null;
while((line = br.readLine())!=null){
strber.append(line+'\n');
}
br.close();
in.close();
result = strber.toString();
if(response.getStatusLine().getStatusCode()!= HttpStatus.SC_OK){
result = "服务器异常";
}
} catch (Exception e){
System.out.println("请求异常");
throw new RuntimeException(e);
} finally{
post.abort();
}
return result;
}