<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.66.Final</version> </dependency> Bootstrap bootstrap = new Bootstrap(); bootstrap.group(new NioEventLoopGroup()) .channel(NioSocketChannel.class); bootstrap.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("encoder", new HttpRequestEncoder()); pipeline.addLast("decoder", new HttpResponseDecoder()); pipeline.addLast("handler", new MyHttpHandler()); } }); ChannelFuture future = bootstrap.connect("api.example.com", 80).sync(); String requestUri = "/api/endpoint"; FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, requestUri); future.channel().writeAndFlush(request); public class MyHttpHandler extends SimpleChannelInboundHandler<FullHttpResponse> { @Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse response) throws Exception { } }


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