HTTP POST请求中文乱码问题解决
来源:互联网 发布:linux while read 编辑:程序博客网 时间:2024/05/16 06:05
先上代码
public static String sendUrl(String urlString, String pram,String readCharset ) {
StringBuffer sb = new StringBuffer();DefaultHttpClient httpclient = new DefaultHttpClient();
BufferedReader reader = null;
try{
// 构造最简单的字符串数据
StringEntity reqEntity = new StringEntity(pram,"utf-8");
HttpPost httppost = new HttpPost(urlString);
httppost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=\"UTF-8\"");
// 设置请求的数据
httppost.setEntity(reqEntity);
// 执行
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// 显示结果
reader = new BufferedReader(new InputStreamReader(entity.getContent(), readCharset));
String line = reader.readLine();
while (line != null) {
sb.append(line);
line = reader.readLine();
}
if (entity != null) {
entity.consumeContent();
}
}catch (Exception e) {
log.error("HttpPostMethodUtil异常",e);
}finally{
try {
if(reader != null){
reader.close();
}
} catch (IOException e) {
log.error("",e);
}
httpclient.getConnectionManager().shutdown();
}
return sb.toString();
}
在网上找的资料说如下两行代码可以解决乱码问题,多次尝试发现并不行。
StringEntity reqEntity = new StringEntity(pram);
reqEntity.setContentEncoding("utf-8");
最后改成如下一行代码,发现解决了中文乱码问题。
StringEntity reqEntity = new StringEntity(pram,"utf-8");
阅读全文
0 0
- HTTP POST请求中文乱码问题解决
- Ajax Post请求中文乱码问题解决方案
- http post 中文乱码
- http 请求中文乱码
- Get请求,Post请求乱码问题解决方案
- 终端请求中文乱码问题解决
- Http请求post方法,请求和响应中文乱码问题的一点解决方法
- httpclient4 post提交请求乱码问题解决
- httpclient4 post提交请求乱码问题解决
- HttpClient post提交请求乱码问题解决
- ajax post请求中文乱码问题解决(不使用escape方法,只使用filter)
- http 请求中的中文乱码
- Android,HTTP请求中文乱码
- Android,HTTP请求中文乱码
- Ajax的POST方法中文乱码问题解决
- GET/POST时中文乱码问题解决方法
- android http post请求,设置utf-8编码,服务端还是出现中文乱码 解决
- spring mvc 发送http post请求返回的中文数据乱码
- UVA
- 小程序如何显示群名称?
- 基础母函数1005
- VS2015设置字体
- anroid 图片处理加载对比(image loader,Picasso ,glide,fresco)
- HTTP POST请求中文乱码问题解决
- Angular2-使用less管理css样式
- CAS实现SSO单点登录原理
- springboot (7) 定时任务
- 列表和元祖
- Cloudera Manager 5 和 CDH5.7.0 本地(离线)安装
- rsync(文件传输工具)
- 继承方式
- Linux虚拟机ip为127.0.0.1的处理