chenjiahe
2022-12-16 5bfcd30741d716cb46bb4540051c3bed65737189
提交 | 用户 | age
f3ad4b 1 package com.hx.util;
C 2
691423 3 import org.apache.commons.io.IOUtils;
C 4
f3ad4b 5 import javax.servlet.http.HttpServletRequest;
C 6 import java.io.BufferedReader;
7 import java.io.IOException;
8 import java.util.Enumeration;
9 import java.util.HashMap;
10 import java.util.Map;
11
12 /** HttpServletRequestUtil  获取请求中的body
13  * @author wangrenhuang
14  * @Date 2021-10-19
15  */
16 public class HttpServletRequestUtil {
17
691423 18     /**
f3ad4b 19     * 获取bady
C 20     * */
21     public static String getBody(HttpServletRequest request) {
691423 22         String wxMsgXml = null;
C 23         if(request == null){
24             return wxMsgXml;
f3ad4b 25         }
691423 26         try{
C 27             wxMsgXml = IOUtils.toString(request.getInputStream(),
28                     "utf-8");
29             return wxMsgXml;
30         }catch (Exception e){
31             wxMsgXml = null;
32         }
33         return wxMsgXml;
f3ad4b 34     }
C 35
36
691423 37     /**
f3ad4b 38     *
C 39     * 获取请求头
40     * */
41     public static Map<String,String> getHeader(HttpServletRequest request) {
42         Enumeration<String> enumeration = request.getHeaderNames();
43         Map<String,String> map = new HashMap<>(16);
44         StringBuffer headers = new StringBuffer();
45         while (enumeration.hasMoreElements()) {
46             String name = enumeration.nextElement();
47             String value = request.getHeader(name);
48             map.put(name,value);
49         }
50
51         return map;
52     }
53 }