NutzCN Logo
问答 Http.httpReq DELETE 请求 似乎 有点异常
发布于 1951天前 作者 Hamming 1726 次浏览 复制 上一个帖子 下一个帖子
标签:

定义如下 工具类 发送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(); }
4 回复

delete还带json?

恩 是这样的

### 2.6 删除临时密码
|URL|DELETE|`/api/temporaryPassword.do`|
|:--:|:--:|:--:|
|参数|`application/json`||
||`deviceNum`|设备编号|
||`identifications`|密码标识数组|
|响应||0-成功,1-失败|
||`UUID`|生成UUID|

示例请求:

curl -X DELETE /api/temporaryPassword.do \
-H 'content-type: application/json' \
-d '{
"deviceNum":"181004020000208",
"identifications": ["1", "2"]
}'
```

```

看来是Sender的bug啦

提交修正了

添加回复
请先登陆
回到顶部