SpringBoot使用httpclient发送Post请求时,字符编码格式问题

public static String post(String url, String params){
        log.info("post url:" + url + " params:" + params);
        String responseStr = "";
        try(CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url);
            StringEntity stringEntity = new StringEntity(params, Charset.forName("UTF-8"));
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setEntity(stringEntity);
            CloseableHttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            responseStr = EntityUtils.toString(entity);
        } catch (IOException e) {
            log.info("post请求失败", e);
        } catch (ParseException e) {
            log.info("post请求失败", e);
        }
        return responseStr;
    }

原文链接 https://blog.csdn.net/u014067137/article/details/118171463

你可能感兴趣的:(SpringBoot使用httpclient发送Post请求时,字符编码格式问题)