获取FullHttpRequest中的请求参数信息
FullHttpRequest
使用Netty开发一个HTTP服务器,Content-Type为 x-www-form-urlencoded的类型,需要从中获取请求参数信息
HTTP请求行
fullRequest.getMethod().name(); //获取请求方法
fullRequest.getMethod().getUri(); //获取请求URI
fullRequest.getProtocolVersion().text() //获取HTTP协议版本
HTTP请求头
private final HttpHeaders header = fullRequest.headers();//获取Netty内置的请求头对象
List<Map.Entry<String, String>> list = header.entries(); //将包含的请求信息赋值到list中
HTTP请求参数
//获取请求体body信息
fullRequest.content().toString(CharsetUtil.UTF_8)
对于Content-Type为 x-www-form-urlencoded的类型,body返回的是一个如下的一个字符串: “key1=value1&key2=value2”,需要手动解析
//解析方法,将字符串解析为一个Map结构
private Map<String, String> getRequestParams(FullHttpRequest request) {
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), request);
List<InterfaceHttpData> httpPostData = decoder.getBodyHttpDatas();
Map<String, String> params = new HashMap<>();
for (InterfaceHttpData data : httpPostData) {
if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
MemoryAttribute attribute = (MemoryAttribute) data;
params.put(attribute.getName(), attribute.getValue());
}
}
return params;
}