public static int patch(String url,JSONObject jsonParam, Map<String, String> header){
int statusCode = 0;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPatch httpPatch = new HttpPatch(url);
//请求头
if (MapUtils.isNotEmpty(header)) {
for (Map.Entry<String, String> entry : header.entrySet()) {
httpPatch.addHeader(entry.getKey(), entry.getValue());
}
}
httpPatch.setHeader("Content-type", "application/json");
try {
if (jsonParam != null){
StringEntity entity = new StringEntity(jsonParam.toString(),StandardCharsets.UTF_8);
httpPatch.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPatch);
statusCode = response.getStatusLine().getStatusCode();
} catch (IOException e) {
log.error(e.getMessage());
}finally {
try {
httpClient.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
return statusCode;
}
转载:Java中利用HttpClient 发送Patch请求,携带Json参数
最新推荐文章于 2024-03-26 17:02:00 发布