postman发送post请求正常,但是还是有乱码.

发布时间:2024年01月10日
未设置编码导致
//调用邮件发送中心,发送邮件
CloseableHttpClient httpClient = HttpClients.createDefault();
String requestBody = null;
try{
    HttpPost httpPost = new HttpPost(EmailAndSmsSendConstant.emailServiceUrl);
    // 设置请求头部信息
    httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
    httpPost.setHeader("keyId", EmailAndSmsSendConstant.keyId);
    // 构建请求体参数
    requestBody = createEmailBody(warnType,subject,body,emailList);

    log.info("执行邮件发送,requestBody:"+requestBody);
    StringEntity entity = new StringEntity(requestBody,"UTF-8"); //增加编码设置
    httpPost.setEntity(entity);
    CloseableHttpResponse response= httpClient.execute(httpPost);
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == 200) {
        return true;
    }else{
        log.error("邮件发送失败,请求参数:"+requestBody+",响应参数:"+JacksonUtils.objectToJson(response));
        return false;
    }

?? ??? ?CloseableHttpClient httpClient = HttpClients.createDefault();
?? ??? ?CloseableHttpResponse response = null;
?? ??? ?MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
?? ??? ?multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
?? ??? ?multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
?? ??? ?JSONObject jsonObject = null;
?? ??? ?String resultString = "";
?? ??? ?try {
?? ??? ??? ?// 创建Http Post请求
?? ??? ??? ?HttpPost httpPost = new HttpPost(url);
?? ??? ??? ?// 创建请求内容
?? ??? ??? ?……
? ? ? ? ? ? List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
? ? ? ? ? ? ……
?? ??? ??? ?httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));

文章来源:https://blog.csdn.net/fengbin2005/article/details/135507451
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。