packagecom.pt.utils;importio.netty.bootstrap.Bootstrap;importio.netty.channel.ChannelFuture;importio.netty.channel.ChannelInitializer;importio.netty.channel.ChannelOption;importio.netty.channel.EventLoopGroup;importio.netty.channel.nio.NioEventLoopGroup;importio.netty.channel.socket.SocketChannel;importio.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.http.*;importio.netty.util.concurrent.DefaultEventExecutorGroup;importio.netty.util.concurrent.EventExecutorGroup;importjava.net.URI;importjava.util.Map;/***@authorpanteng
* @description
* @date 17-3-20.*/
public classNonBlockHttpClient {public static EventLoopGroup workerGroup = new NioEventLoopGroup(1);public static Bootstrap b = newBootstrap();public static final EventExecutorGroup executor = new DefaultEventExecutorGroup(2);static{
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,1000);
}public static Object lock = newObject();/*** 异步GET请求
*
*@paramurl
*@paramhead
*@paramhandler
*@return
*/
public static Boolean get(String url, Map head, finalHttpHandler handler) {try{
URI uri= newURI(url);
String domain=uri.getHost();
Integer port= uri.getPort() < 0 ? 80: uri.getPort();
DefaultFullHttpRequest request= newDefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString());if (head == null) {
request.headers().add("Host", domain);
request.headers().add("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0");
request.headers().add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
request.headers().add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
request.headers().add("Connection", "keep-alive");
request.headers().add("Cache-Control", "max-age=0");
}else{for(Map.Entry entry : head.entrySet()) {
request.headers().add((String) entry.getKey(), entry.getValue());
}
}
ChannelInitializer channelInitializer= new ChannelInitializer() {
@Overrideprotected void initChannel(SocketChannel socketChannel) throwsException {//客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码
socketChannel.pipeline().addLast(newHttpResponseDecoder());//客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码
socketChannel.pipeline().addLast(newHttpRequestEncoder());
socketChannel.pipeline().addLast(executor,newGeneralHandler(handler));
}
};
ChannelFuture f;synchronized(lock) {
b.handler(channelInitializer);
f=b.connect(domain, port).sync();
}
f.channel().writeAndFlush(request);
}catch(Exception e) {
e.printStackTrace();
}return false;
}public static voidclose() {try{
executor.shutdownGracefully();
workerGroup.shutdownGracefully();
}catch(Exception e) {
e.printStackTrace();
}
}
}