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<String,String> getHeader(HttpServletRequest request) {
|
Enumeration<String> enumeration = request.getHeaderNames();
|
Map<String,String> 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;
|
}
|
}
|