java端微信公众号发送模板信息乱码解决

jdk1.8 windows环境,tomcat下,发送模板信息乱码

项目采用httpclient发送请求

java端微信公众号发送模板信息乱码解决_第1张图片

解决方案

 StringEntity s = new StringEntity(jsonStr,"UTF-8");这里需要设置utf-8

    public static String postStr(String url,String jsonStr){
        
    	CloseableHttpClient httpclient = HttpClients.createDefault();
    	HttpPost post = new HttpPost(url);
        String response = null;
        try {
            StringEntity s = new StringEntity(jsonStr,"UTF-8");
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");//发送json数据需要设置contentType
            post.setEntity(s);
            LOGGER.info("executing request " + post.getURI());
            LOGGER.info("" + jsonStr);
            HttpResponse res = httpclient.execute(post);

            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                String result = EntityUtils.toString(res.getEntity());// 
                response = result;
                LOGGER.info("Response content: " + result);
            }
        } catch (Exception e) {
        	LOGGER.info("出现错误");
            throw new RuntimeException(e);
        }
        return response;
    }
}

 

 

 

你可能感兴趣的:(java)