Java获取请求头信息

第一步,获取request ,response

     RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();//这个RequestContextHolder是Springmvc提供来获得请求的东西
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) requestAttributes).getResponse();

第二步,封装方法,获取请求头信息

 private   Map  getHeads(HttpServletRequest request){
        Map stringObjectHashMap = new HashMap<>();
        Enumeration headers = request.getHeaderNames();
        System.out.println("请求头信息");
        while(headers.hasMoreElements()){
            String headName = (String)headers.nextElement();
            String headValue = request.getHeader(headName);
            System.out.println(headName+":"+headValue);
            stringObjectHashMap.put(headName,headValue);
        }
        return stringObjectHashMap;
    }

你可能感兴趣的:(Java,java)