HttpClient中文乱码

来源:互联网 发布:淘宝店铺旺旺号怎么看 编辑:程序博客网 时间:2024/05/16 05:32


使用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);

0 0
原创粉丝点击