package com.hx.util; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; import java.io.IOException; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; /** HttpServletRequestUtil 获取请求中的body * @author wangrenhuang * @Date 2021-10-19 */ public class HttpServletRequestUtil { /* * 获取bady * */ public static String getBody(HttpServletRequest request) { BufferedReader br = null; StringBuilder sb = new StringBuilder(""); String str ; try { br = request.getReader(); while((str = br.readLine()) != null){ sb.append(str); } } catch (IOException e) { e.printStackTrace(); }finally { try { if (null != br){ br.close(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } /* * * 获取请求头 * */ public static Map getHeader(HttpServletRequest request) { Enumeration enumeration = request.getHeaderNames(); Map map = new HashMap<>(16); StringBuffer headers = new StringBuffer(); while (enumeration.hasMoreElements()) { String name = enumeration.nextElement(); String value = request.getHeader(name); map.put(name,value); } return map; } }