Java 无效的HTTP方法:修补程序
在尝试了其他解决方案之后 我得到了无效的HTTP方法:Java7的补丁异常。 更新JAVA不在选项中,因此我必须坚持解决方法 我使用调用来像这样调用请求Java 无效的HTTP方法:修补程序,java,http,httpurlconnection,java-7,Java,Http,Httpurlconnection,Java 7,在尝试了其他解决方案之后 我得到了无效的HTTP方法:Java7的补丁异常。 更新JAVA不在选项中,因此我必须坚持解决方法 我使用调用来像这样调用请求 Invocation invoke = reqBuilder.build(getHTTPVerb(), Entity.entity(requestJSON, MediaType.APPLICATION_JSON)); getWebTarget().request(MediaType.APPLICATION_JSON).header("
Invocation invoke = reqBuilder.build(getHTTPVerb(), Entity.entity(requestJSON, MediaType.APPLICATION_JSON));
getWebTarget().request(MediaType.APPLICATION_JSON).header("Authorization", getAuthorization()).accept(MediaType.APPLICATION_JSON);
getHTTPVerb()
返回字符串“POST”或“PATCH”
使用补丁方法时,我遇到了问题
在上述问题中,我没有尝试过一种解决方案:
conn.setRequestProperty("X-HTTP-Method-Override", "PATCH");
conn.setRequestMethod("POST");
conn
是HttpURLConnection
实例
但我不确定如何从调用类或任何属性获取
HttpURLConnection
非常感谢您提供任何指导或帮助。apache http客户端的补丁方法示例:
try {
//This is just to avoid ssl hostname verification and to trust all, you can use simple Http client also
CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
HttpPatch request = new HttpPatch(REST_SERVICE_URL);
StringEntity params = new StringEntity(JSON.toJSONString(payload), ContentType.APPLICATION_JSON);
request.setEntity(params);
request.addHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
request.addHeader(HttpHeaders.AUTHORIZATION, OAuth2AccessToken.BEARER_TYPE + " " + accessToken);
HttpResponse response = httpClient.execute(request);
String statusCode = response.getStatusLine().getStatusCode();
} catch (Exception ex) {
// handle exception here
}
RestTemplate的等效示例:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", OAuth2AccessToken.BEARER_TYPE + " " + accessToken);
final HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(payload), headers);
RestTemplate restTemplate = new RestTemplate();
try {
ResponseEntity<String> response = restTemplate.exchange(REST_SERVICE_URL, HttpMethod.PATCH, entity, String.class);
String statusCode = response.getStatusCode();
} catch (HttpClientErrorException e) {
// handle exception here
}
HttpHeaders=newhttpheaders();
headers.setContentType(MediaType.APPLICATION_JSON);
添加(“授权”,OAuth2AccessToken.BEARER_TYPE+“”+accessToken);
最终HttpEntity实体=新的HttpEntity(JSON.toJSONString(payload),头文件);
RestTemplate RestTemplate=新RestTemplate();
试一试{
ResponseEntity response=restemplate.exchange(REST\u SERVICE\u URL,HttpMethod.PATCH,entity,String.class);
字符串statusCode=response.getStatusCode();
}捕获(HttpClientErrorE异常){
//在这里处理异常
}
apache http客户端的补丁方法示例:
try {
//This is just to avoid ssl hostname verification and to trust all, you can use simple Http client also
CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
HttpPatch request = new HttpPatch(REST_SERVICE_URL);
StringEntity params = new StringEntity(JSON.toJSONString(payload), ContentType.APPLICATION_JSON);
request.setEntity(params);
request.addHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
request.addHeader(HttpHeaders.AUTHORIZATION, OAuth2AccessToken.BEARER_TYPE + " " + accessToken);
HttpResponse response = httpClient.execute(request);
String statusCode = response.getStatusLine().getStatusCode();
} catch (Exception ex) {
// handle exception here
}
RestTemplate的等效示例:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", OAuth2AccessToken.BEARER_TYPE + " " + accessToken);
final HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(payload), headers);
RestTemplate restTemplate = new RestTemplate();
try {
ResponseEntity<String> response = restTemplate.exchange(REST_SERVICE_URL, HttpMethod.PATCH, entity, String.class);
String statusCode = response.getStatusCode();
} catch (HttpClientErrorException e) {
// handle exception here
}
HttpHeaders=newhttpheaders();
headers.setContentType(MediaType.APPLICATION_JSON);
添加(“授权”,OAuth2AccessToken.BEARER_TYPE+“”+accessToken);
最终HttpEntity实体=新的HttpEntity(JSON.toJSONString(payload),头文件);
RestTemplate RestTemplate=新RestTemplate();
试一试{
ResponseEntity response=restemplate.exchange(REST\u SERVICE\u URL,HttpMethod.PATCH,entity,String.class);
字符串statusCode=response.getStatusCode();
}捕获(HttpClientErrorE异常){
//在这里处理异常
}
您是否尝试过中建议从httpcomponents使用HttpClient的解决方案?另请参见HttpURLConnection
只是不支持补丁
。您必须使用不同的HTTP库。@Redlab是的,我试过了,我在问题中也提到了相同的帖子。不过,来自httpcomponents的HttpPatch可以做到这一点……您是否尝试过中建议使用来自httpcomponents的HttpClient的解决方案?另请参见HttpURLConnection
只是不支持补丁
。您必须使用不同的HTTP库。@Redlab是的,我试过了,我在问题中也提到了同一篇文章。不过,来自httpcomponents的HttpPatch可以做到这一点。。。。