java - 无法通过 JAVA 对 w REST API 进行 PATCH
问题描述
我想用不同的方法调用 API:
使用 HttpClient 发布和获取都可以
我无法执行 PATCH 和 delete 方法,有没有人实现过这样的事情?如何 ?
发布方法1
public static String sendPost(String requestURL, Map<String, String> headers, String postParameters,
boolean withProxy) throws IOException {
HttpURLConnection con = createProxyHttpConnection(requestURL, withProxy);
con.setRequestMethod("POST");
con.setDoOutput(true);
for (Map.Entry<String, String> entry : headers.entrySet()) {
con.setRequestProperty(entry.getKey(), entry.getValue());
}
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postParameters);
String response = IOUtils.toString(con.getInputStream(), "UTF-8");
wr.close();
con.disconnect();
return response;
}
发布方法 2
public static HttpResponse sendPostBis(String requestURL, Map<String, String> headers, String payload,
boolean withProxy) throws IOException {
StringEntity sEntity = new StringEntity(payload,
ContentType.APPLICATION_FORM_URLENCODED);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(requestURL);
for (Map.Entry<String, String> entry : headers.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
request.setEntity(sEntity);
HttpResponse response = httpClient.execute(request);
return response;
}
我将方法 1 用于带有参数的 POST,将方法 2 用于带有 json body 的 POST
错误消息 (如果我在 SoapUI 中将方法更改为 POST 而不是 PATCH,我将收到相同的消息)
{"error":"No route found for \u0022POST \RESOURCE","message":"No route found for \u0022POST RESOURCE"}
解决方案
在不提供任何代码的情况下,我最好的猜测是您尝试发出这些 PATCH 和 DELETE 请求的网络已经阻止了这些 HTTP 动词,因此您无法发出它们。大多数网络安全工具认为除 GET 和 POST 之外的任何动词都是不安全的,因此会将它们列入黑名单
推荐阅读
- javascript - 从对象数组中删除除最后一个相似键之外的所有键
- ios - Flutter - 插件,如何依赖 ios 本地 pod
- java - 使用来自 dto 的对象数组构建 HttpMethod POST 请求
- javascript - vanilla javascript:将父元素更改为输入
- c++ - 在我的代码中添加新变量以及一些操作后,堆栈粉碎检测到 C++
- cheat-engine - 是否可以将游戏价值整合到程序中?
- sql - 在 SQL 中使用 2 个表查找最新状态
- javascript - 如果在某个语音频道中,需要帮助断开成员的连接
- pandas - 根据日期范围重复数据框行
- javascript - 请求验证后 Laravel 返回无效格式