Java Netty不发送某些字符序列
我开始熟悉Netty,因为我计划在未来的项目中使用它 但我偶然发现了一些奇怪的行为 因为我将在项目中使用文本协议,所以我从带有StringDecoder、StringEncoder和DelimiterBaseFrameDecoder的标准文本管道开始。但现在我将其简化为以下内容:Java Netty不发送某些字符序列,java,netty,Java,Netty,我开始熟悉Netty,因为我计划在未来的项目中使用它 但我偶然发现了一些奇怪的行为 因为我将在项目中使用文本协议,所以我从带有StringDecoder、StringEncoder和DelimiterBaseFrameDecoder的标准文本管道开始。但现在我将其简化为以下内容: EventLoopGroup workerGroup = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(w
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new TestClientHandler());
}
});
ChannelFuture f = b.connect("some.working.web.server", 80).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
ctx.writeAndFlush(Unpooled.copiedBuffer(
" HTTP/1.0\n", CharsetUtil.US_ASCII));
到
然后,请求被发送到服务器,服务器当然会拒绝请求并返回错误
我更多地使用了请求字符串,出于某种原因,Netty似乎不喜欢以下内容:
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new TestClientHandler());
}
});
ChannelFuture f = b.connect("some.working.web.server", 80).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
ctx.writeAndFlush(Unpooled.copiedBuffer(
" HTTP/1.0\n", CharsetUtil.US_ASCII));
无法发送此字符串。但是删除前导空格或更改随机字符会将字符串发送到服务器
更奇怪的是,如果我用SMTP服务器进行测试,请求将毫无问题地发送到服务器。唯一的区别是SMTP服务器在我的字符串发送到服务器之前发送HELO消息
Java 1.7.0_21和1.8.0_20上的Netty 4.0.23和4.1.0.Beta3的行为也相同。
如果我改为OioEventLoopGroup和OioSocketChannel,也会保持不变
同时稍微更改channelActive方法:
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channel active");
ByteBuf bb = Unpooled.buffer();
bb.writeBytes("GET index.htm HTTP/1.0\r\n".getBytes());
System.out.println(ByteBufUtil.hexDump(bb));
ctx.writeAndFlush(bb);
System.out.println("after write");
}
显示我的请求已转换为正确的字节序列:
47455420696e6465782e68746d20485454502f312e300d0a
G E T i n d e x . h t m H T T P / 1 . 0 \r\n
如果有人能解释一下这个问题,我将不胜感激。这个问题原来是由防病毒软件引起的。 显然,我正在使用的软件正在使用一个黑客解析器进行HTTP监控,它吃掉了我的请求,而我的请求实际上是无效的。。。结尾缺少额外的\r\n 关闭防病毒软件解决了这个问题。并将请求更正为: GET/index.html HTTP/1.0\r\n\r\n 我一直很高兴 但是为什么它决定吃呢 GET/index.html HTTP/1.0\r\n 而不是 GET/index.html HTTPA/1.0\r\n 我无法理解
47455420696e6465782e68746d20485454502f312e300d0a
G E T i n d e x . h t m H T T P / 1 . 0 \r\n