如何在netty中序列化和反序列化HttpMessage或HttpContent?

时间:2014-02-11 21:34:14

标签: java netty

我正在使用netty5。并且,我正在尝试读取http标头以从http连接获取信息,然后根据标头信息将所有消息从该通道重新发送到另一个目标。有没有办法将HttpContent和HttpMessage序列化/反序列化为字节数组?或者有更好的方法吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

如果您想获取请求标题,请参阅netty源代码中的示例代码段:HttpSnoopServerHandler.java

public class HttpSnoopServerHandler extends SimpleChannelInboundHandler<Object> {

    @Override
    protected void messageReceived(ChannelHandlerContext ctx, Object msg) {
        if (msg instanceof HttpRequest) {
            HttpRequest request = this.request = (HttpRequest) msg;
            HttpHeaders headers = request.headers();
            if (!headers.isEmpty()) {
                for (Map.Entry<String, String> h: headers) {
                    String key = h.getKey();
                    String value = h.getValue();
                    buf.append("HEADER: ").append(key).append(" = ").append(value).append("\r\n");
                }
                buf.append("\r\n");
                ...
            }
            ...
        }
    }
}