我正在制作一台Netty Http服务器,而且我遇到了一个问题。每当我从浏览器或java程序发送请求时,它都会发送两次(是的,我在我的管道中使用HttpObjectAggregator
。)例如:
INFO: [id: 0x8f2a1b64, L:/0:0:0:0:0:0:0:0:8080] READ: [id: 0x006ab104, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:62690]
Jul 19, 2017 12:52:58 PM io.netty.handler.logging.LoggingHandler channelReadComplete
INFO: [id: 0x8f2a1b64, L:/0:0:0:0:0:0:0:0:8080] READ COMPLETE
Jul 19, 2017 12:52:58 PM io.netty.handler.logging.LoggingHandler channelRead
INFO: [id: 0x8f2a1b64, L:/0:0:0:0:0:0:0:0:8080] READ: [id: 0xbf604bb0, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:62691]
Jul 19, 2017 12:52:58 PM io.netty.handler.logging.LoggingHandler channelReadComplete
INFO: [id: 0x8f2a1b64, L:/0:0:0:0:0:0:0:0:8080] READ COMPLETE
message: testmesstwoo
HttpObjectAggregator$AggregatedFullHttpRequest(decodeResult: success, version: HTTP/1.1, content: CompositeByteBuf(ridx: 0, widx: 0, cap: 0, components=0))
GET /?message=testmesstwoo HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
content-length: 0s
Send failed2: null
//This is where the second request is:
no message
HttpObjectAggregator$AggregatedFullHttpRequest(decodeResult: success, version: HTTP/1.1, content: CompositeByteBuf(ridx: 0, widx: 0, cap: 0, components=0))
GET /favicon.ico HTTP/1.1
Host: localhost:8080
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Accept: image/webp,image/*,*/*;q=0.8
Referer: http://localhost:8080/?message=testmesstwoo
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
content-length: 0s
Send failed2: null
这一切都来自一个浏览器请求。
这是我的处理程序channelRead方法:
public void channelRead0(ChannelHandlerContext ctx, Object msg) { // (2)
if(!(msg instanceof HttpRequest)) {
System.out.println("not http");
System.out.println(msg.toString());
System.out.println(msg.getClass());
//ctx.write("hi");
return;
}
HttpRequest request = (HttpRequest) msg;
QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
Map<String, java.util.List<String>> params = queryStringDecoder.parameters();
try {
System.out.println("message: "+ params.get("message").get(0));
}catch(Exception e) {
//e.printStackTrace();
System.out.println("no message");
}
ByteBuf bytes = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8);
DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,bytes);
resp.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
//resp.addHeader(, buffer.readableBytes());
//ctx.writeAndFlush("hi");
//ChannelFuture cf=ctx.writeAndFlush(Unpooled.copiedBuffer("Ack", CharsetUtil.UTF_8));
System.out.println(msg+"s");
HttpUtil.setContentLength(resp, 4);
ChannelFuture cf=ctx.writeAndFlush(resp);
System.out.println("Send failed2: " + cf.cause());
}
这真让我难过,所以任何帮助都会非常感激。如果我需要提供更多代码,请告诉我。
编辑:
这是我的管道:
//p.addLast(new HttpRequestDecoder());
// Uncomment the following line if you don't want to handle HttpChunks.
p.addLast(new HttpServerCodec());
p.addLast(new HttpObjectAggregator(1048576));
//p.addLast(new HttpResponseEncoder());
// Remove the following line if you don't want automatic content compression.
//p.addLast(new HttpContentCompressor());
p.addLast(new Handler());