package http;
import constant.Constant;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
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.HttpClientCodec;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
public class HttpClient {
public static final String HOST = "127.0.0.1";
public static void main(String[] args) throws InterruptedException {
if (HttpServer.SSL) {
System.out.println("服务器处于SSL模式,客户端不支持,推出");
return ;
}
HttpClient client = new HttpClient();
client.connect(Constant.DEFAULT_SERVER_IP, Constant.DEFAULT_PORT);
}
public void connect(String host, int port) throws InterruptedException {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new HttpClientCodec());
ch.pipeline().addLast("aggregator",
new HttpObjectAggregator(10 * 1024 * 1024));
ch.pipeline().addLast("decompressor", new HttpContentDecompressor());
ch.pipeline().addLast(new HttpClientInboundHandler());
}
});
ChannelFuture f = b.connect(host, port).sync();
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
System.out.println("连接成功....");
}
});
ChannelFuture closeFuture = f.channel().closeFuture().sync();
closeFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
System.out.println("关闭成功...");
}
});
} finally {
workerGroup.shutdownGracefully();
}
}
}
package http;
import constant.Constant;
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;
public class HttpClientInboundHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
FullHttpResponse httpResponse = (FullHttpResponse) msg;
System.out.println(httpResponse.status());
System.out.println(httpResponse.headers());
ByteBuf buf = httpResponse.content();
System.out.println(buf.toString(CharsetUtil.UTF_8));
httpResponse.release();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelActive.......");
URI uri = new URI("/test");
String msg = "Hello";
DefaultFullHttpRequest request =
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.GET,
uri.toASCIIString(),
Unpooled.wrappedBuffer(msg.getBytes("UTF-8")));
request.headers().set(HttpHeaderNames.HOST, Constant.DEFAULT_SERVER_IP);
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
request.headers().set(HttpHeaderNames.CONTENT_LENGTH,
request.content().readableBytes());
ctx.writeAndFlush(request);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
package http;
import java.util.Random;
public class RespConstant {
private static final String[] NEWS = {
"她那时候还太年轻,不知道所有命运赠送的礼物,早已在暗中标好了价格。——斯蒂芬·茨威格《断头皇后》",
"这是一个最好的时代,也是一个最坏的时代;这是一个智慧的年代,这是一个愚蠢的年代;\n" +
"这是一个信任的时期,这是一个怀疑的时期;这是一个光明的季节,这是一个黑暗的季节;\n" +
"这是希望之春,这是失望之冬;人们面前应有尽有,人们面前一无所有;\n" +
"人们正踏上天堂之路,人们正走向地狱之门。 —— 狄更斯《双城记》",
};
private static final Random R = new Random();
public static String getNews() {
return NEWS[R.nextInt(NEWS.length)];
}
}
package constant;
import java.util.Date;
public class Constant {
public static final Integer DEFAULT_PORT = 7777;
public static final String DEFAULT_SERVER_IP= "127.0.0.1";
public static String response(String msg) {
return "Hello, " + msg + ", Now is" + new Date(System.currentTimeMillis()).toString();
}
}