- FullHttpRequest
- HTTP请求行
- HTTP请求头
- HTTP请求参数
使用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内置的请求头对象 ListHTTP请求参数> list = header.entries(); //将包含的请求信息赋值到list中
//获取请求体body信息 fullRequest.content().toString(CharsetUtil.UTF_8)
对于Content-Type为 x-www-form-urlencoded的类型,body返回的是一个如下的一个字符串: “key1=value1&key2=value2”,需要手动解析
//解析方法,将字符串解析为一个Map结构
private Map getRequestParams(FullHttpRequest request) {
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), request);
List httpPostData = decoder.getBodyHttpDatas();
Map 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;
}



