Repository navigation
Expand file tree
/
Copy pathNettyHttpClient.java
More file actions
51 lines (51 loc) · 2.03 KB
/
Copy pathNettyHttpClient.java
File metadata and controls
51 lines (51 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// package io.github.kimmking.gateway.outbound;
//
// import io.netty.bootstrap.Bootstrap;
// import io.netty.channel.ChannelFuture;
// import io.netty.channel.ChannelInitializer;
// import io.netty.channel.ChannelOption;
// import io.netty.channel.EventLoopGroup;
// import io.netty.channel.nio.NioEventLoopGroup;
// import io.netty.channel.socket.SocketChannel;
// import io.netty.channel.socket.nio.NioSocketChannel;
// import io.netty.handler.codec.http.HttpRequestEncoder;
// import io.netty.handler.codec.http.HttpResponseDecoder;
//
// public class NettyHttpClient {
// public void connect(String host, int port) throws Exception {
// EventLoopGroup workerGroup = new NioEventLoopGroup();
//
// try {
// Bootstrap b = new Bootstrap();
// b.group(workerGroup);
// b.channel(NioSocketChannel.class);
// b.option(ChannelOption.SO_KEEPALIVE, true);
// b.handler(new ChannelInitializer<SocketChannel>() {
// @Override
// public void initChannel(SocketChannel ch) throws Exception {
// // 客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码
// ch.pipeline().addLast(new HttpResponseDecoder());
// // 客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码
// ch.pipeline().addLast(new HttpRequestEncoder());
// ch.pipeline().addLast(new HttpClientOutboundHandler());
// }
// });
//
// // Start the client.
// ChannelFuture f = b.connect(host, port).sync();
//
//
// f.channel().write(request);
// f.channel().flush();
// f.channel().closeFuture().sync();
// } finally {
// workerGroup.shutdownGracefully();
// }
//
// }
//
// public static void main(String[] args) throws Exception {
// NettyHttpClient client = new NettyHttpClient();
// client.connect("127.0.0.1", 8844);
// }
// }