Netty5.0 获取http get、post请求参数

private Map<String, String> getRequestParams(ChannelHandlerContext ctx, HttpRequest req){

            Map<String, String>requestParams=new HashMap<>();
            // 处理get请求 
            if (req.getMethod() == GET) {
                QueryStringDecoder decoder = new QueryStringDecoder(req.getUri());  
                Map<String, List<String>> parame = decoder.parameters();  
                Iterator<Entry<String, List<String>>> iterator = parame.entrySet().iterator();
                while(iterator.hasNext()){
                    Entry<String, List<String>> next = iterator.next();
                    requestParams.put(next.getKey(), next.getValue().get(0));
                }
            }
             // 处理POST请求 
            if (req.getMethod() == POST) {
                HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(  
                        new DefaultHttpDataFactory(false), req);  
                List<InterfaceHttpData> postData = decoder.getBodyHttpDatas(); //
                for(InterfaceHttpData data:postData){
                    if (data.getHttpDataType() == HttpDataType.Attribute) {  
                        MemoryAttribute attribute = (MemoryAttribute) data;  
                        requestParams.put(attribute.getName(), attribute.getValue());
                    }
                }
            }
            return requestParams;
        }

你可能感兴趣的:(http,参数,post,get,netty5)