HttpClient发起请求中文数据乱码问题解决


原来部分代码如下:
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
try {
StringEntity s = new StringEntity(jsonStr);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
CloseableHttpResponse res = client.execute(post);

修改如下,在初始化StringEntity时指定UTF-8:
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
try {
StringEntity s = new StringEntity(jsonStr,"UTF-8");
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
CloseableHttpResponse res = client.execute(post);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM