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

社会主义新天地 提交于 2019-12-17 05:07:52


     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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!