Netty HttpServerCodec和HttpObjectAggregator

GET请求

Get请求包括两个部分:

request line(包括method,request uri,protocol version))

header

HttpServerCodec是netty针对http编解码的处理类,但是这些只能处理像http get的请求,也就是数据带在url问号后面的http请求参数

POST请求

POST请求包括三个部分

request line(包括method,request uri,protocol version))

header

message body

从上可以看出,当我们用POST方式请求服务器的时候,对应的参数信息是保存在message body中的,如果只是单纯的用HttpServerCodec是无法完全的解析Http POST请求的,因为HttpServerCodec只能获取uri中参数,所以需要加上HttpObjectAggregator。

HttpObjectAggregator这个netty的处理器就是为了解决这个问题而来的.它把HttpMessage和HttpContent聚合成为一个FullHttpRquest或者FullHttpRsponse,大致结构如下图所示:

复制代码
public class ChannelHandlerInitializer extends ChannelInitializer<SocketChannel> {

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {

		// HTTP编码解码器
		ch.pipeline().addLast("http-codec", new HttpServerCodec());
		// 把HTTP头、HTTP体拼成完整的HTTP请求
		ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
        // httpRequestHandler 中获取到的就是一个完整的Http消息了
		ch.pipeline().addLast("http-handler", httpRequestHandler);
	}

}
相关推荐
兆子龙1 天前
ahooks useRequest 深度解析:一个 Hook 搞定所有请求
java·javascript
兆子龙1 天前
React Suspense 从入门到实战:让异步加载更优雅
java·javascript
咕白m6251 天前
Java 实现 Excel 转 HTML:完整示例
java
RealPluto1 天前
Spring AOP 失效排查
java·spring
码路飞1 天前
热榜全是 OpenClaw,但我用 50 行 Python 就造了个桌面 AI Agent 🤖
java·javascript
Nyarlathotep01131 天前
LinkedList源码分析
java·后端
用户8307196840821 天前
Java 告别繁琐数据统计代码!MySQL 8 窗口函数真香
java·sql·mysql
带刺的坐椅2 天前
SolonCode v0.0.20 发布 - 编程智能体(新增子代理和浏览器能力)
java·ai·agent·solon·solon-ai·claude-code·openclaw
会员源码网2 天前
数字格式化陷阱:如何优雅处理 NumberFormatException
java