转载:Java中利用HttpClient 发送Patch请求,携带Json参数

public static int patch(String url,JSONObject jsonParam, Map header){
        int statusCode = 0;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPatch httpPatch = new HttpPatch(url);
        //请求头
        if (MapUtils.isNotEmpty(header)) {
            for (Map.Entry 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)