JAVA获取POST请求的请求头信息


     public static byte[] getRequestPostBytes(HttpServletRequest request)
                throws IOException {
            int contentLength = request.getContentLength();
            if(contentLength<0){
                return null;
            }
            byte buffer[] = new byte[contentLength];
            for (int i = 0; i < contentLength;) {
 
                int readlen = request.getInputStream().read(buffer, i,
                        contentLength - i);
                if (readlen == -1) {
                    break;
                }
                i += readlen;
            }
            return buffer;
        }
 
        /**      
         * 描述:获取 post 请求内容
         * <pre>
         * 举例:
         * </pre>
         * @param request
         * @return
         * @throws IOException      
         */
        public static String getRequestPostStr(HttpServletRequest request)
                throws IOException {
            byte buffer[] = getRequestPostBytes(request);
            String charEncoding = request.getCharacterEncoding();
            if (charEncoding == null) {
                charEncoding = "UTF-8";
            }
            return new String(buffer, charEncoding);
        }
 
        /**      
         * 描述:获取请求头内容    
         */
        private String getHeadersInfo(HttpServletRequest request) {
            Map<String, String> map = new HashMap<String, String>();
            Enumeration headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String key = (String) headerNames.nextElement();
                String value = request.getHeader(key);
                map.put(key, value);
            }
            
            String result="";
            for (String key : map.keySet()) {  
                   //System.out.println("key= "+ key + " and value= " + map.get(key));  
                result = result + "key= "+ key + " and value= " + map.get(key)+"\n";
                  }  
            return result;
          }

点赞
————————————————
版权声明:本文为CSDN博主「Vc_004」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/vc_004/article/details/78110261

猜你喜欢

转载自blog.csdn.net/qq_27088383/article/details/103569655