EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpClientCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new HttpContentDecompressor()); pipeline.addLast(new HttpClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect("example.com", 80).sync(); if (channelFuture.isSuccess()) { Channel channel = channelFuture.channel(); FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"); channel.writeAndFlush(request).sync(); } @Override protected void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; // ... } } channel.closeFuture().sync(); eventLoopGroup.shutdownGracefully();


上一篇:
下一篇:
切换中文