定义如下 工具类 发送DELETE请求
public static String deletePassword(String token, String lockNo, String phone){
NutMap head = new NutMap();
head.addv("Authorization", token);
head.addv("Content-Type", "application/json");
NutMap parm = new NutMap();
parm.addv("deviceNum", lockNo);
parm.addv("identifications", Arrays.asList(phone));
String parmStr = Json.toJson(parm);
Response resp = Http.httpReq("https://paas.locksuiyi.com/deviceManagement/api/temporaryPassword.do",
Request.METHOD.DELETE,
parmStr,
Header.create(head),
50000,
Sender.Default_Conn_Timeout);
if(!resp.isOK()){
log.error("设置删除密码失败 锁编号:" + lockNo + " 用户:" + phone + " " + resp.getContent());
}
return resp.getContent();
}
String token="eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxIiwiaWF0IjoxNTY2Mjg3MTAwLCJhZG1pbklkIjoxMTgsImV4cCI6MTU2ODg3OTEwMH0._fWi_IZytnQw09_OMBhzoqLJT9DbWNjzpgUSAn3LdZ4";
String deviceNum ="181201020000124";
String identifications = "17600261502";
String re = LockUtils.deletePassword(token,deviceNum,identifications);
请求 失败 返回 400
自定义 请求 DELETE却能成功
public static String sendDeleteReq(String url,String token, String param) {
PrintWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try {
log.info("sendPostJson - {}", url);
//HTTP URL类 用这个类来创建连接
URL httpUrl = new URL(url);
//建立连接
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setRequestMethod("DELETE");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Authorization", token);
//设置不要缓存
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
log.info("recv - {}", result);
} catch (ConnectException e) {
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
} catch (SocketTimeoutException e) {
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
} catch (IOException e) {
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
} catch (Exception e) {
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
}
}
return result.toString();
}