1、Client類 建立連接
package com.bokeyuan.http.client; import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.*; import java.net.InetSocketAddress; /** * @author: void * @date: 2021-09-10 15:27 * @description: 客戶端 建立連接 * @version: 1.0 */ public class Client { private String ip; private int port; public Client(String ip, int port) { this.ip = ip; this.port = port; } public void start() throws InterruptedException { //線程組 EventLoopGroup group = new NioEventLoopGroup(); //啟動類 Bootstrap bootstrap = new Bootstrap(); try { bootstrap.group(group) .remoteAddress(new InetSocketAddress(ip, port)) //長連接 .option(ChannelOption.SO_KEEPALIVE, true) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel channel) throws Exception { //包含編碼器和解碼器 channel.pipeline().addLast(new HttpClientCodec()); //聚合 channel.pipeline().addLast(new HttpObjectAggregator(1024 * 10 * 1024)); //解壓 channel.pipeline().addLast(new HttpContentDecompressor()); channel.pipeline().addLast(new ClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } finally { group.shutdownGracefully(); } } public static void main(String[] args) throws InterruptedException { Client client = new Client("127.0.0.1",8899); client.start(); } }
2、ClientHandler類 發送http請求
channelActive()中發送http請求,channelRead()方法中處理響應報文
package com.chenly.bokeyuan.http.client; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.codec.http.*; import io.netty.util.CharsetUtil; import java.net.URI; import java.nio.charset.StandardCharsets; /** * @author: void * @date: 2021-09-10 15:28 * @description: * @version: 1.0 */ public class ClientHandler extends ChannelInboundHandlerAdapter { /** * 客戶端與服務端建立連接時執行 * @param ctx * @throws Exception */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { //發送請求至服務端 URI url = new URI("/test"); String msg = "hello"; //配置HttpRequest的請求數據和一些配置信息 FullHttpRequest request = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.GET, url.toASCIIString(), Unpooled.wrappedBuffer(msg.getBytes(StandardCharsets.UTF_8))); request.headers() .set(HttpHeaderNames.CONTENT_TYPE,"text/plain;charset=UTF-8") //開啟長連接 .set(HttpHeaderNames.CONNECTION,HttpHeaderValues.KEEP_ALIVE) //設置傳遞請求內容的長度 .set(HttpHeaderNames.CONTENT_LENGTH,request.content().readableBytes()); ctx.writeAndFlush(request); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { FullHttpResponse response = (FullHttpResponse) msg; ByteBuf content = response.content(); HttpHeaders headers = response.headers(); System.out.println("content:"+content.toString(CharsetUtil.UTF_8)); System.out.println("headers:"+headers.get("content-type").toString()); } }