使用httpclient传输中文的时候遇到乱码,设置的setContentEncoding("UTF-8"),但还是不行,最终修改StringEntity entity = new StringEntity(data,"UTF-8");这样设置成功了。
代码如下:
HttpPost post = new HttpPost(url);
StringEntity entity = new StringEntity(data,"UTF-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
post.setEntity(entity);
RequestConfig config = RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(config).build();
HttpResponse response = httpClient.execute(post);