无法通过 JAVA 在 w REST API 上执行 PATCH
Unable to do a PATCH on w REST API through JAVA
我想用不同的方法调用 API :
post和get都可以使用HttpClient
我无法执行 PATCH 和删除方法,有没有人实现过这样的事情?以及如何?
Post方法一
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;
}
Post方法二
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 用于 POST 和 json body
错误消息 (如果我将方法更改为 POST 而不是 SoapUI 中的 PATCH,我将收到相同的消息)
{"error":"No route found for \u0022POST \RESOURCE","message":"No route found for \u0022POST RESOURCE"}
在不提供任何代码的情况下,我的最佳猜测是您尝试发出这些 PATCH 和 DELETE 请求的网络已阻止这些 HTTP 动词,因此您无法发出它们。大多数网络安全工具认为 GET 和 POST 以外的任何动词都是不安全的,因此将它们列入黑名单
已解决:
将 HttpPost request = new HttpPost(requestURL);
更改为 HttpPatch request = new HttpPatch(requestURL);
我的 url (https) 也有问题,谢谢@Ivan Jadric
我想用不同的方法调用 API :
post和get都可以使用HttpClient
我无法执行 PATCH 和删除方法,有没有人实现过这样的事情?以及如何?
Post方法一
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;
}
Post方法二
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 用于 POST 和 json body
错误消息 (如果我将方法更改为 POST 而不是 SoapUI 中的 PATCH,我将收到相同的消息)
{"error":"No route found for \u0022POST \RESOURCE","message":"No route found for \u0022POST RESOURCE"}
在不提供任何代码的情况下,我的最佳猜测是您尝试发出这些 PATCH 和 DELETE 请求的网络已阻止这些 HTTP 动词,因此您无法发出它们。大多数网络安全工具认为 GET 和 POST 以外的任何动词都是不安全的,因此将它们列入黑名单
已解决:
将 HttpPost request = new HttpPost(requestURL);
更改为 HttpPatch request = new HttpPatch(requestURL);
我的 url (https) 也有问题,谢谢@Ivan Jadric