java 接受和发送http,post,get请求,参数中文乱码解决。
来源:互联网 发布:电子处方软件app 编辑:程序博客网 时间:2024/05/22 11:34
maven 坐标
<!-- HTTP --><dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.1</version></dependency>
一,发送post和get请求
http工具类
public class HttpUtils { /** * 发送 get请求 */ public static String get(String param,String URL) { CloseableHttpClient httpclient = HttpClients.createDefault(); String obj=null; try { HttpUriRequest httpget = new HttpGet(URL+"?"+param); // 执行get请求. CloseableHttpResponse response = httpclient.execute(httpget); try { // 获取响应实体 HttpEntity entity = response.getEntity(); // 打印响应状态 System.out.println(response.getStatusLine()); if (entity != null) { obj=EntityUtils.toString(entity); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } }return obj; } /** * 发送 post请求 */ public static String post(String json,String URL) { String obj=null; // 创建默认的httpClient实例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httppost HttpPost httppost = new HttpPost(URL); httppost.addHeader("Content-type", "application/json; charset=utf-8"); httppost.setHeader("Accept", "application/json"); try { StringEntity s = new StringEntity(json,Charset.forName("UTF-8")); //对参数进行编码,防止中文乱码 s.setContentEncoding("UTF-8"); httppost.setEntity(s); CloseableHttpResponse response = httpclient.execute(httppost); try { //获取相应实体 HttpEntity entity = response.getEntity(); if (entity != null) { obj=EntityUtils.toString(entity, "UTF-8"); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return obj;} }
json工具类
public final class JSONUtil {public static<T> String toJson(T t){return JSON.toJSONString(t);}public static<T> T parseJson(String text,Class<T> clazz){return JSON.parseObject(text, clazz);}}控制层
//发送post请求Obj obj=new Obj()//参数 任意实体类HttpUtils.post(JSONUtil.toJson(obj), "http://www.baidu.com");//发送get请求String param="param1=" + URLEncoder.encode("中国", "UTF-8") + "¶m2=value2";HttpUtils.get(param, "http://www.baidu.com");二 接受 http请求
接收post请求
//接收post请求需要加@RequestBody @RequestMapping(value="test",method=RequestMethod.POST) @ResponseBody public MyResult<Object> test(HttpServletRequest request,@RequestBody String record){MyResult<Object> result = new MyResult<Object>(); Obj obj=JSONUtil.parseJson(record, Obj.class); return result; }接收get请求
@RequestMapping(value="test") @ResponseBody public MyResult<Object> test(HttpServletRequest request,String param1,String param2){ MyResult<Object> result = new MyResult<Object>(); //TODO return result; }
阅读全文
1 0
- java 接受和发送http,post,get请求,参数中文乱码解决。
- 解决 AngularJs $http 发送post,get请求,SpringMvc无法接受参数的问题。
- java发送http get 和 post 请求
- 安卓发送Get、Post请求,解决参数乱码问题
- 解决了中文乱码的http的get和post请求demo
- java发送HTTP请求 (get和post方法请求)
- post和get请求中文参数出现乱码现象解决方案
- JAVA 发送Http请求,GET、POST请求
- 解决Get和Post请求中中文乱码问题
- 解决Get和post请求中中文乱码问题
- 解决Get和Post请求中文乱码的问题
- Java后台发送Post和get请求 (并解决返回值乱码问题)
- get post 中文请求乱码解决方式
- Java解决各种中文乱码问题(1)get和post请求
- Java各种中文乱码问题的解决(1)get和post请求
- Java 获取Http Get请求中文参数乱码解决方案
- java 发送HTTP (get 和 post方法请求)
- Java发送HTTP的POST和GET方式请求
- 在Unity中实现GPS定位(获取用户地理位置,不使用第三方插件)
- 读书笔记
- qsort的compare函数
- 4.利用socket实现聊天机器人
- Python+paramiko基于配置文件的批量主机管理
- java 接受和发送http,post,get请求,参数中文乱码解决。
- 剑指offer_二叉树---二叉树的深度
- HTTP返回码详解
- 大数据01_高并发的处理Nginx
- .Net使用对象关系映射框架Entity Framework操作数据库及其内容
- Excel 2016新增图表攻略
- sql查询时间段内所有周六日
- 如何移除Chrome浏览器让人不爽的GoogleUpdate.exe后台更新进程
- 工作三年后,我选择离开腾讯