關於後臺http請求地址的問題
阿新 • • 發佈:2018-12-05
@Transactional(readOnly = false) public IotControlCommandRecord ControlSendJidiPost(IotControlDeviceData iotControlDeviceData, String URL) { StringBuilder result = new StringBuilder(); BufferedReader in = null; IotControlCommandRecord iotControlCommandRecord = new IotControlCommandRecord(); try { logger.info(URL); iotControlCommandRecord.setMsg("通訊中介軟體連線異常!"); URL url = new URL(URL); // 開啟url連線 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 設定url請求方式 ‘get’ 或者 ‘post’ connection.setRequestMethod("POST"); // 傳送 in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); String line = ""; while ((line = in.readLine()) != null) { result.append(line); } logger.info(result.toString()); String str = "false"; if (result.toString().contains(str)) { return iotControlCommandRecord; } else { iotControlCommandRecord.setMsg("控制命令已傳送"); iotControlCommandRecord.setC(iotControlDeviceData.getCtlsn()); iotControlCommandRecord.setG(iotControlDeviceData.getGatewaysn()); iotControlCommandRecord.setE(iotControlDeviceData.getDevno()); // iotControlCommandRecord.setT(Integer.valueOf(iotControlDeviceData.getT() + // "")); if (iotControlDeviceData.getState()) { iotControlCommandRecord.setM("1"); } else { iotControlCommandRecord.setM("0"); } iotControlCommandRecordService.save(iotControlCommandRecord); } } catch (IOException e) { logger.error("URL 異常:" + e.getMessage()); e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); logger.error("關閉流失敗" + e.getMessage()); } } return iotControlCommandRecord; } public String SetJiDiUrl(CtdDevice ctdDevice, String switchData, Integer slaveId) { // "http://222.143.21.116:8085/CTDFishing/mqtt/sendDeviceControlCommand?deviceID=0000010&deviceKey=NJcasyml&msgType=4&msgData.addr="+ // id + "&msgData.dout="+addr, String url = Global.getConfig("JiDiURL") + "sendDeviceControlCommand?deviceID=" + ctdDevice.getDeviceId() + "&deviceKey=" + ctdDevice.getDevicekey() + "&msgType=4&msgData.addr=" + slaveId + "&msgData.dout=" + switchData; return url; }
這是一個傳送遠端控制命令控制裝置的程式,先拼接出來一個字串url,