0

我正在使用 Apache HttpCompones 将这样的数据发布到 Netty。我使用 Gson 来处理 JSON。

Request.Post("http://localhost:9090/print").bodyString(getJSon(), ContentType.APPLICATION_JSON),

这个 netty 代码似乎没有收到 JSON 响应。我确信我的代码是错误的。这里可能是什么错误?

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
                                                    throws Exception {
    logger.info( "channelRead" + msg );
    if( msg instanceof HttpRequest ){
        this.request = ( HttpRequest ) msg;
        logger.info( "[" + request.getUri()  + "]");
    }
    if( msg instanceof HttpContent ){
        HttpContent content = (HttpContent)msg;
        ByteBuf buf = content.content();
        logger.info( "[" + buf.toString(CharsetUtil.UTF_8)   + "]");
           if (msg instanceof LastHttpContent) {
                LastHttpContent trailer = (LastHttpContent) msg;
                writeResponse( trailer, ctx);
           }
    }
}



private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
    logger.info( getJSon() );
    ByteBuf response = Unpooled.copiedBuffer( getJSon(),
                                              CharsetUtil.UTF_8);

    ctx.write( response );
    /* Where is 'isKeepAlive' method in the API ? */
    //boolean keepAlive = isKeepAlive(request);
    //keepAlive;
    return false;  
}
4

1 回答 1

0

您需要调用 ctx.writeAndFlush(...) 将其实际刷新到套接字。

于 2013-10-07T18:28:40.277 回答