HttpClient 请求参数中文编码问题
2017-12-25 15:56
591 查看
目录
问题
问题分析
初看之下没有问题,平时使用也经常看不出问题(因为程序员在作请求都会本能规避请求参数中含有中文内容),但是一旦传参
可以看出是典型的
可以看到,这个
尼玛,固有的全是ISO_8859_1,要中文啊,还好有
问题
问题分析
问题
首先看下我们正常使用HttpClient作POST请求的处理方法public static String doHttpPost(String url, JSONObject params) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httpPost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); params.forEach((key, value) -> { if (value != null) { nvps.add(new BasicNameValuePair(key, String.valueOf(value))); } else { nvps.add(new BasicNameValuePair(key, "")); } }); HttpEntity reqEntity = EntityBuilder .create() .setContentEncoding('UTF-8") .setParameters(nvps).build(); httpPost.setEntity(reqEntity); CloseableHttpResponse response = httpclient.execute(httpPost); HttpEntity entity = response.getEntity(); String result = null; if (entity != null) { result = EntityUtils.toString(entity, "UTF-8"); } EntityUtils.consume(entity); response.close(); return result; } finally { httpclient.close(); } }
初看之下没有问题,平时使用也经常看不出问题(因为程序员在作请求都会本能规避请求参数中含有中文内容),但是一旦传参
params含有中文,就会在收到的请求中看到
???这种分分钟让程序猿高潮的东西!因为
setContentEncoding('UTF-8")这一句根本没起作用
问题分析
请求参数的处理主要集中在下面这部分HttpEntity reqEntity = EntityBuilder.create().setContentEncoding('UTF-8").setParameters(nvps).build();
可以看出是典型的
装饰者模式,通过Maven查看
EntityBuilder源码中的
build()函数如下:
public HttpEntity build() { final AbstractHttpEntity e; //... 略 ... else if (this.parameters != null) { e = new UrlEncodedFormEntity(this.parameters, this.contentType != null ? this.contentType.getCharset() : null); } //... 略 ... e.setContentEncoding(this.contentEncoding); //... 略 ... return e; }
可以看到,这个
setContentEncoding不是设置请求参数编码的,参数编码设置是
e = new UrlEncodedFormEntity(this.parameters,this.contentType != null ? this.contentType.getCharset() : null);这一句完成的,所以参数编码要用
setContentType()来设置。再来看
ContentType的源码
public static final ContentType APPLICATION_ATOM_XML = create( "application/atom+xml", Consts.ISO_8859_1); public static final ContentType APPLICATION_FORM_URLENCODED = create( "application/x-www-form-urlencoded", Consts.ISO_8859_1); public static final ContentType APPLICATION_JSON = create( "application/json", Consts.UTF_8); public static final ContentType APPLICATION_OCTET_STREAM = create( "application/octet-stream", (Charset) null); public static final ContentType APPLICATION_SVG_XML = create( "application/svg+xml", Consts.ISO_8859_1); public static final ContentType APPLICATION_XHTML_XML = create( "application/xhtml+xml", Consts.ISO_8859_1); public static final ContentType APPLICATION_XML = create( "application/xml", Consts.ISO_8859_1); public static final ContentType MULTIPART_FORM_DATA = create( "multipart/form-data", Consts.ISO_8859_1); public static final ContentType TEXT_HTML = create( "text/html", Consts.ISO_8859_1); public static final ContentType TEXT_PLAIN = create( "text/plain", Consts.ISO_8859_1); public static final ContentType TEXT_XML = create( "text/xml", Consts.ISO_8859_1); public static final ContentType WILDCARD = create( "*/*", (Charset) null);
尼玛,固有的全是ISO_8859_1,要中文啊,还好有
ContentType.create(mimeType, charset)可以用,所以最终完成的代码如下:
public static String doHttpPost(String url, JSONObject params) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httpPost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); params.forEach((key, value) -> { if (value != null) { nvps.add(new BasicNameValuePair(key, String.valueOf(value))); } else { nvps.add(new BasicNameValuePair(key, "")); } }); HttpEntity reqEntity = EntityBuilder .create() .setContentType( ContentType.create( "application/x-www-form-urlencoded", Consts.UTF_8)).setParameters(nvps).build(); httpPost.setEntity(reqEntity); CloseableHttpResponse response = httpclient.execute(httpPost); HttpEntity entity = response.getEntity(); String result = null; if (entity != null) { result = EntityUtils.toString(entity, "UTF-8"); } EntityUtils.consume(entity); response.close(); return result; } finally { httpclient.close(); } }
相关文章推荐
- Android使用HttpUrlConnection get请求带中文参数的编码问题
- Http协议中关于请求与响应中文参数乱码问题详解
- 解决HttpURLConnection请求时传中文参数乱码问题
- Request对象中请求参数中文编码问题
- httpclient 的 doPost请求中文乱码问题
- HttpClient Get请求中返回中文乱码问题解决
- HttpClient3 POST的中文编码问题
- HttpClient 中文编码问题
- Spring Tomcat Post Get 请求参数有中文时出现乱码或+号变空格等关于编码的问题
- Spring学习笔记(三十五):关于httpClient的post请求,到后台中文乱码的问题
- HttpClient POST的中文编码问题
- HttpClient4.5.2版本中POST提交中文参数时乱码问题解决方法
- http请求参数中中文乱码问题解决办法
- httpclient post 上传文件及参数包括中文乱码问题方案
- HttpClient4.4.1 关于文件上传 中文文件名编码问题
- Http Url Get请求方式需要对中文参数进行编码
- HttpClient POST的中文编码问题
- Http Post请求提交json格式数据工具类,解决请求参数中文乱码问题
- HttpClient POST的中文编码问题
- HttpClient Post和Get请求实例+ 中文参数处理