POST请求的长度

时间:2014-09-21 11:12:23

标签: java post request netty

我正在使用Netty Http Server,但我发现了一个问题。

简而言之:当我通过POST AJAX请求发送内容时,Netty服务器只从请求的内容中捕获前55个字符。这是我的代码:

public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException, InterruptedException
{
    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;
        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            String message = content.toString(CharsetUtil.UTF_8);
            System.out.println(message);
        }
    }
}

初始化类如下:

public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {

    private final SslContext sslCtx;

    public HttpServerInitializer(SslContext sslCtx) {
        this.sslCtx = sslCtx;
    }

    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline p = ch.pipeline();
        if (sslCtx != null) {
            p.addLast(sslCtx.newHandler(ch.alloc()));
        }
        p.addLast("aggregator", new HttpObjectAggregator(1048576));
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpServerHandler());
    }
}

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案。 initChannel()方法应该包含以下代码:

public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }
    p.addLast("decoder", new HttpRequestDecoder());
    p.addLast("aggregator", new HttpObjectAggregator(1048576));
    p.addLast("encoder", new HttpResponseEncoder());
    p.addLast(new HttpServerHandler());
}