diff --git a/02nio/nio01/pom.xml b/02nio/nio01/pom.xml
index e42a62f2..5925690d 100644
--- a/02nio/nio01/pom.xml
+++ b/02nio/nio01/pom.xml
@@ -59,6 +59,12 @@
netty-all
4.1.51.Final
+
+ org.apache.httpcomponents
+ httpasyncclient
+ 4.1.4
+
+
diff --git a/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java b/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java
index 81f31d2e..9b124b0c 100644
--- a/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java
+++ b/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java
@@ -9,6 +9,15 @@
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.util.ReferenceCountUtil;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.concurrent.FutureCallback;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
+import org.apache.http.util.EntityUtils;
import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION;
import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
@@ -17,7 +26,7 @@
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
public class HttpHandler extends ChannelInboundHandlerAdapter {
-
+
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
@@ -51,6 +60,13 @@ private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx,
// httpGet ... http://localhost:8801
// 返回的响应,"hello,nio";
// value = reponse....
+ String url = "http://localhost:8801";
+ final HttpGet httpGet = new HttpGet(url);
+
+ CloseableHttpClient httpclient = HttpClients.createDefault();
+ CloseableHttpResponse httpResponse = httpclient.execute(httpGet);
+ HttpEntity entity = httpResponse.getEntity();
+ value = EntityUtils.toString(entity, "UTF-8");
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8")));
response.headers().set("Content-Type", "application/json");
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpRequestFilter.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpRequestFilter.java
index af5c37fb..b90443bc 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpRequestFilter.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpRequestFilter.java
@@ -8,5 +8,6 @@ public class HeaderHttpRequestFilter implements HttpRequestFilter {
@Override
public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
fullRequest.headers().set("mao", "soul");
+ fullRequest.headers().set("xjava", "Geektime");
}
}
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java
index c20c9be5..6c8624b4 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java
@@ -84,6 +84,7 @@ private void fetchGet(final FullHttpRequest inbound, final ChannelHandlerContext
//httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
httpGet.setHeader("mao", inbound.headers().get("mao"));
+ httpGet.setHeader("xjava", inbound.headers().get("xjava"));
httpclient.execute(httpGet, new FutureCallback() {
@Override
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java
index 79aeb148..ffa42590 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java
@@ -1,51 +1,50 @@
-//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() {
-// @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);
-// }
-//}
\ No newline at end of file
+package io.github.kimmking.gateway.outbound.netty4;
+
+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() {
+ @Override
+ public void initChannel(SocketChannel ch) throws Exception {
+ // 客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码
+ ch.pipeline().addLast(new HttpResponseDecoder());
+ // 客户端发送的是http request,所以要使用HttpRequestEncoder进行编码
+ ch.pipeline().addLast(new HttpRequestEncoder());
+ ch.pipeline().addLast(new NettyHttpClientOutboundHandler());
+ }
+ });
+
+ // Start the client.
+ ChannelFuture f = b.connect(host, port).sync();
+
+ f.channel().write("");
+ 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", 8801);
+ }
+}
\ No newline at end of file
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClientOutboundHandler.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClientOutboundHandler.java
index 6730cd5a..f9b5b766 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClientOutboundHandler.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClientOutboundHandler.java
@@ -1,22 +1,51 @@
package io.github.kimmking.gateway.outbound.netty4;
+import static io.netty.handler.codec.http.HttpHeaderNames.*;
+import static io.netty.handler.codec.http.HttpResponseStatus.OK;
+import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
+
+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.*;
+
+public class NettyHttpClientOutboundHandler extends ChannelInboundHandlerAdapter {
+
+ private HttpRequest request;
-public class NettyHttpClientOutboundHandler extends ChannelInboundHandlerAdapter {
-
@Override
public void channelActive(ChannelHandlerContext ctx)
throws Exception {
-
-
+
+
}
-
+
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
-
-
-
+
+ if (msg instanceof HttpRequest) {
+ request = (HttpRequest) msg;
+
+ String uri = request.uri();
+ System.out.println("Uri:" + uri);
+ }
+ if (msg instanceof HttpContent) {
+ HttpContent content = (HttpContent) msg;
+ ByteBuf buf = content.content();
+ System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8));
+ buf.release();
+
+ FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(msg.toString().getBytes("UTF-8")));
+ response.headers().set(CONTENT_TYPE, "text/plain");
+ response.headers().set(CONTENT_LENGTH,
+ response.content().readableBytes());
+ response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
+
+ ctx.write(response);
+ ctx.flush();
+ }
+
}
}
\ No newline at end of file