chenjiahe
2023-01-10 bcc2fa8323a61920c7d2a6139c38fc25b17baa7f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.hx.util;
 
import org.apache.commons.io.IOUtils;
 
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) {
        String wxMsgXml = null;
        if(request == null){
            return wxMsgXml;
        }
        try{
            wxMsgXml = IOUtils.toString(request.getInputStream(),
                    "utf-8");
            return wxMsgXml;
        }catch (Exception e){
            wxMsgXml = null;
        }
        return wxMsgXml;
    }
 
 
    /**
    *
    * 获取请求头
    * */
    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;
    }
}