2014-09-21 81 views
0

我正在使用Netty Http Server,但我發現了一個問題。簡而言之:當我通過POST AJAX請求發送內容時,Netty服務器只捕獲請求內容的前55個字符。這是我的代碼:POST請求的長度

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()); 
    } 
} 
+0

「的Netty服務器只捕獲前55個*圖表*」:你的意思是_chars_,如字符? – Krumia 2014-09-21 11:22:33

+0

是Krumia,characters :) – Simon 2014-09-21 11:33:37

+0

您是否增加了標準HTTP消息參數,這些參數是對象/解碼器的默認值?例如新的HttpObjectAggregator(512 * 1024) – paziwatts 2014-09-21 11:37:48

回答

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()); 
}