Skip to content

Commit 01109ee

Browse files
committed
必做作业:整合你上次作业的 httpclient/okhttp
1 parent 6943786 commit 01109ee

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

02nio/nio01/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
<artifactId>netty-all</artifactId>
6060
<version>4.1.51.Final</version>
6161
</dependency>
62+
<dependency>
63+
<groupId>org.apache.httpcomponents</groupId>
64+
<artifactId>httpasyncclient</artifactId>
65+
<version>4.1.4</version>
66+
</dependency>
67+
6268
</dependencies>
6369

6470

02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
import io.netty.handler.codec.http.FullHttpResponse;
1010
import io.netty.handler.codec.http.HttpUtil;
1111
import io.netty.util.ReferenceCountUtil;
12+
import org.apache.http.HttpEntity;
13+
import org.apache.http.HttpResponse;
14+
import org.apache.http.client.methods.CloseableHttpResponse;
15+
import org.apache.http.client.methods.HttpGet;
16+
import org.apache.http.concurrent.FutureCallback;
17+
import org.apache.http.impl.client.CloseableHttpClient;
18+
import org.apache.http.impl.client.HttpClients;
19+
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
20+
import org.apache.http.util.EntityUtils;
1221

1322
import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION;
1423
import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
@@ -17,7 +26,7 @@
1726
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
1827

1928
public class HttpHandler extends ChannelInboundHandlerAdapter {
20-
29+
2130
@Override
2231
public void channelReadComplete(ChannelHandlerContext ctx) {
2332
ctx.flush();
@@ -51,6 +60,13 @@ private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx,
5160
// httpGet ... http://localhost:8801
5261
// 返回的响应,"hello,nio";
5362
// value = reponse....
63+
String url = "http://localhost:8801";
64+
final HttpGet httpGet = new HttpGet(url);
65+
66+
CloseableHttpClient httpclient = HttpClients.createDefault();
67+
CloseableHttpResponse httpResponse = httpclient.execute(httpGet);
68+
HttpEntity entity = httpResponse.getEntity();
69+
value = EntityUtils.toString(entity, "UTF-8");
5470

5571
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8")));
5672
response.headers().set("Content-Type", "application/json");

0 commit comments

Comments
 (0)