spring mvc 发送http post请求返回的中文数据乱码
来源:互联网 发布:起点西游记知乎 编辑:程序博客网 时间:2024/06/05 10:49
今天在做项目的时候,发现发送http post请求返回的数据中文乱码,在网上搜了一下,找到一个相关的资源,结果发现用不了,最后自己试着改了一下,终于解决了这个乱码的问题。
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
解决方法:
将in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 改成 in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
- spring mvc 发送http post请求返回的中文数据乱码
- spring mvc 发送http post请求返回的中文数据乱码
- Spring MVC的Post请求参数中文乱码解决方法
- java实现发送HTTP的POST请求,返回数据以及请求状态
- 模拟http发送post请求并返回数据
- spring mvc post 请求乱码
- ajax请求spring mvc controller,返回中文字符串显示乱码
- jQuery 发送 post 请求返回字符串乱码的解决
- spring mvc 返回中文乱码
- 客户端发送的post请求,request中的中文乱码问题
- 关于http请求返回数据中文乱码解决方法
- SpringMvc发送post请求中文乱码问题
- Spring MVC后台返回JSON数据中文乱码问题解决
- Spring MVC Controller中返回json数据中文乱码处理
- Spring mvc 使用Gson 返回Json数据 中文乱码问题
- Spring MVC Controller中返回json数据中文乱码处理
- Spring MVC Controller中返回json数据中文乱码处理
- Spring MVC Controller中返回json数据中文乱码处理
- 数论学习笔记 基础数论(未完成)
- A. Memory and Crow
- Programming In Scala笔记-第六章、函数式对象
- Leetcode 169. Majority Element[easy]
- 死锁的必要条件?怎么克服?
- spring mvc 发送http post请求返回的中文数据乱码
- EditText,Button
- eng、user、userdebug区别简述
- Android27--Android之ActioBar
- Android Google推荐的图片加载库Glide介绍
- 【补题计划】913模拟赛第一题——maze2
- 编程之美_cpu使用率
- 使用Unity加载外部视频
- DirectX学习笔记(九):模板缓存与镜面效果实现