EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpClientCodec()); pipeline.addLast(new HttpObjectAggregator(8192)); pipeline.addLast(new SimpleChannelInboundHandler<FullHttpResponse>() { @Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) { } }); } }); URI uri = new URI("http://www.example.com"); String host = uri.getHost(); int port = uri.getPort(); ChannelFuture future = bootstrap.connect(host, port).sync(); HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath()); future.channel().writeAndFlush(request).sync(); future.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); }


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