我想用不同的方法调用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;
}
我使用带参数的POST方法1和带json正文的POST方法2
错误消息(如果我将方法更改为POST而不是SoapUI中的PATCH,则会收到相同的消息)
{"error":"No route found for \u0022POST \RESOURCE","message":"No route found for \u0022POST RESOURCE"}
最佳答案
如果不提供任何代码,我的最佳猜测是您试图通过其发出PATCH和DELETE请求的网络已阻止了这些HTTP动词,因此您无法发出它们。大多数网络安全工具认为GET和POST以外的任何动词都是不安全的,因此会将它们列入黑名单