java 获取 HttpServletRequest 值 demo

 1     private void getHttpServletRequestInfo(HttpServletRequest request){
 2 
 3         try {
 4             StringBuilder stringBuilder = new StringBuilder();
 5             stringBuilder.append("--------------------------reqHeadInfos---------------------------------");
 6             Enumeration<String> reqHeadInfos = request.getHeaderNames();
 7             while (reqHeadInfos.hasMoreElements()) {
 8                 String headName = (String) reqHeadInfos.nextElement();
 9                 String headValue = request.getHeader(headName);//根据请求头的名字获取对应的请求头的值
10                 stringBuilder.append(headName).append(":").append(headValue).append(";");
11             }
12 
13             stringBuilder.append("\n--------------------------parameterNames---------------------------------\n");
14             Enumeration<String> parameterNames = request.getParameterNames();
15             while (parameterNames.hasMoreElements()) {
16                 String parameterName = (String) parameterNames.nextElement();
17                 String parameterValue = request.getParameter(parameterName);//根据请求头的名字获取对应的请求头的值
18                 stringBuilder.append(parameterName).append(":").append(parameterValue).append(";");
19             }
20             stringBuilder.append("\n--------------------------body---------------------------------\n");
21             BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
22             String body = reader.readLine();
23             stringBuilder.append("body:").append(body).append(";");
24 
25             stringBuilder.append("\n--------------------------Session---------------------------------\n");
26             HttpSession httpSession = request.getSession();
27             stringBuilder.append("SessionID:").append(httpSession.getId()).append(";");
28             Enumeration<String> attributeNames =  httpSession.getAttributeNames();
29             while (attributeNames.hasMoreElements()) {
30                 String parameterName = (String) attributeNames.nextElement();
31                 Object parameterValue = httpSession.getAttribute(parameterName);//根据请求头的名字获取对应的请求头的值
32                 stringBuilder.append(parameterName).append(":").append(parameterValue.toString()).append(";");
33             }
34             stringBuilder.append("\n--------------------------Cookie---------------------------------\n");
35             Cookie[] cookies = request.getCookies();
36             if(cookies != null){
37                 for (Cookie cookie : cookies) {
38                     String cookieName = cookie.getName();
39                     String cookieValue = cookie.getValue();//根据Cookie的名字获取对应的请求头的值
40                     stringBuilder.append(cookieName).append(":").append(cookieValue).append(";");
41                 }
42             }
43 
44             stringBuilder.append("\n----------------------------other-------------------------------\n");
45             stringBuilder.append("characterEncoding:").append(request.getCharacterEncoding()).append(";");
46             stringBuilder.append("getContentLength:").append(request.getContentLength()).append(";");
47             stringBuilder.append("getContentType:").append(request.getContentType()).append(";");
48             stringBuilder.append("getAuthType:").append(request.getAuthType()).append(";");
49             stringBuilder.append("getMethod:").append(request.getMethod()).append(";");
50 
51             stringBuilder.append("isRequestedSessionIdValid:").append(request.isRequestedSessionIdValid()).append(";");
52             stringBuilder.append("isRequestedSessionIdFromCookie:").append(request.isRequestedSessionIdFromCookie()).append(";");
53             stringBuilder.append("isRequestedSessionIdFromURL:").append(request.isRequestedSessionIdFromURL()).append(";");
54 
55             Log.info("getHttpServletRequestInfo",stringBuilder.toString());
56 
57         } catch (Exception e) {
58             Log.error("getHttpServletRequestInfo", e);
59         }
60     }
原文地址:https://www.cnblogs.com/baoconghui/p/7359014.html