发送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 @ 2022-11-17 17:37  xiaolifc  阅读(63)  评论(0)    收藏  举报