HttpClient用HttpPost传输中文字符串乱码
2016-01-06 20:01
465 查看
public static String getHttpRequestString(String url,String body) throws IOException { HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(body); httpPost.setEntity(stringEntity); httpPost.setHeader("Content-Type", "application/json; charset=UTF-8"); HttpResponse response = client.execute(httpPost); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line; StringBuffer jsonString = new StringBuffer(); while((line = bufferedReader.readLine()) != null) { jsonString.append(line); } return jsonString.toString(); }
这是最初的代码,如果传输的body有中文汉字的话,如果对方设置的格式是UTF-8,那么他接收到的字符是乱码,
stringEntity.setContentEncoding("UTF-8");
加上这样一句代码,设置下格式就好了。
相关文章推荐
- Java中发送http的get、post请求
- TCP三次握手,四次挥手
- iOS打开本地和网络上的word、ppt、excel等文件
- JSON,ListView AsyncHttpClient 等第三方包的综合实应用
- 卷积神经网络CNN在自然语言处理的应用
- 算法杂货铺——分类算法之贝叶斯网络(Bayesian networks)
- 前篇:3.团队初建,与设计师建立设计规范。http://www.ui.cn/detail/67329.html
- Linux学习之Center os网络配置
- NSIS DirectUI界面 nsSkinEngine 插件,让安装包不再枯燥,一键打包!!http://www.aceui.cn/
- 基于神经网络和遗传算法的【手写数字识别】机器人的实现
- 网络接口获取下来的时间转换成NSDate
- 互联网协议 网络通信
- HTTP协议
- 监测网络状态
- Android-Volley网络通信框架
- HTTP协议
- 运输层TCP协议详细介绍
- 使用shiro的session后,HttpSession.getServletContext() 获取一直是null的解决方法
- 网络编程基础
- HTTP 错误 500.19 - Internal Server Error