发送http请求时中文乱码

刚开始发送http请求时,中文传输的字段一直是乱码,找不到问题,后来查阅资料后才知道设置UTF-8有可能失败,需要在StringEntity处设置

原来代码

/**
* 发https请求,json做参数
*
* @param url url
* @param map json参数
* @return result
*/
public static String HttpsPostSendJson(String url, String map) {
        String result = null;
        try {
            //HttpClient httpClient = new SSLClient();

            HttpPost httpPost = new HttpPost(url);
            //设置参数
            httpPost.addHeader("Accept", "application/json");
            httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
            StringEntity stringEntity = new StringEntity(map);

            RequestConfig config = RequestConfig.custom()
                    .setSocketTimeout(REQUEST_TIMEOUT)
                    .setConnectTimeout(REQUEST_TIMEOUT)
                    .build();

            httpPost.setConfig(config);
            httpPost.setEntity(stringEntity);

            CloseableHttpClient httpClient = HttpClients.custom()
                    .setSSLSocketFactory(createSSLConnSocketFactory())
                    .setDefaultRequestConfig(config)
                    .build();

            HttpResponse response = httpClient.execute(httpPost);

            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, "utf-8");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return result;
    }

更改后代码

/**
* 发https请求,json做参数
*
* @param url url
* @param map json参数
* @return result
*/
  public static String HttpsPostSendJson(String url, String map) {
        String result = null;
        try {
            //HttpClient httpClient = new SSLClient();

            HttpPost httpPost = new HttpPost(url);
            //设置参数
            httpPost.addHeader("Accept", "application/json");
            httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");

            StringEntity stringEntity = new StringEntity(map,"UTF-8");

            //stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType("application/json");


            RequestConfig config = RequestConfig.custom()
                    .setSocketTimeout(REQUEST_TIMEOUT)
                    .setConnectTimeout(REQUEST_TIMEOUT)
                    .build();

            httpPost.setConfig(config);
            httpPost.setEntity(stringEntity);

            CloseableHttpClient httpClient = HttpClients.custom()
                    .setSSLSocketFactory(createSSLConnSocketFactory())
                    .setDefaultRequestConfig(config)
                    .build();

            HttpResponse response = httpClient.execute(httpPost);

            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, "utf-8");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return result;
    }

经测试,发送中文不再显示乱码.

posted @   xiaolifc  阅读(56)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示