netty的HttpResponseEncoder使得无法发送数据

qq_41583211 2018-04-24 07:58:35
新人在用netty写一个http服务器,目前发现服务器的httpRespondse无法发送到客户端上,只有去掉HttpResponseEncoder才可以将信息发出,烦请各位大神指点一下问题出在哪里

目前服务器可以顺利接收到客户端的request,正确读取post请求,部分代码如下:

ch.pipeline().addLast(new HttpRequestDecoder());
ch.pipeline().addLast(new HttpServerCodec());
ch.pipeline().addLast(new HttpObjectAggregator(1024*1024));
ch.pipeline().addLast(new JsonObjectDecoder());
ch.pipeline().addLast(new HttpResponseEncoder());
ch.pipeline().addLast(new HttpContentCompressor());
ch.pipeline().addLast(new HttpServerHandler()); //自己写的handler,用来处理业务

这是与Respondse的处理:

FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
Unpooled.wrappedBuffer(j.toString().getBytes("UTF-8"))); //j为json数据
response.headers().set("Content-Length", res.toString().length());
response.headers().set("Content-Type","application/json");
response.headers().set("Connection", "keep-alive");
ctx.writeAndFlush(response);



...全文
1027 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_41583211 2018-04-27
  • 打赏
  • 举报
回复
过了三天依然没有解决问题 .. 目前发现自己写的程序里, response - body 处于整个fullresponse的开头,而不是结尾,不知道这个和目前的问题是否有关 希望大神们能指点一下
qq_41583211 2018-04-24
  • 打赏
  • 举报
回复
目前还是没能解决ch.pipeline().addLast(new HttpResponseEncoder()); 之后无法发送respondse的问题。 如果删去这条语句,对于一次request,客户端会触发两次clientHttpHandler的channelRead0(): 客户端通道:

ch.pipeline().addLast(new HttpResponseDecoder());
ch.pipeline().addLast(new HttpRequestEncoder());
ch.pipeline().addLast(new clientHttpHandler());
第一次: DefaultHttpResponse(decodeResult: success, version: HTTP/1.1) HTTP/1.1 400 Bad Request Content-Length: 12 Content-Type: application/json Connection: keep-alive 第二次: DefaultLastHttpContent(data: PooledSlicedByteBuf(ridx: 0, widx: 12, cap: 12/12, unwrapped: PooledUnsafeDirectByteBuf(ridx: 116, widx: 118, cap: 1024)), decoderResult: success)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧