I just started netty and I am really disappointed with the documentation provided on their website.
I am trying to connect to a URL using Netty .. I took a sample client from your site and changed it to suit my requirement.
The code:
public class NettyClient { public static void main(String[] args) throws Exception { String host = "myUrl.com/v1/parma?param1=value"; int port = 443; 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 { ch.pipeline().addLast(new ClientHandler()); ch.pipeline().addLast("encoder", new HttpRequestEncoder()); } });
But the problem is that it only expects a URL without request parameters. How to pass request parameters using url? and please provide me with a link to some good documentation for Netty 4 ..
EDIT
Client code after linking to the example mentioned in the answer:
URI uri = new URI("myUrl.com/v1/parma?param1=value"); String scheme = uri.getScheme() == null? "http" : uri.getScheme(); String host = "myUrl.com"; int port = 443; boolean ssl = "https".equalsIgnoreCase(scheme);
handler code:
public class ClientHandler extends SimpleChannelInboundHandler<HttpObject> { @Override public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception { if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; System.out.println("STATUS: " + response.getStatus()); System.out.println("VERSION: " + response.getProtocolVersion()); System.out.println(); if (!response.headers().isEmpty()) { for (String name: response.headers().names()) { for (String value: response.headers().getAll(name)) { System.out.println("HEADER: " + name + " = " + value); } } System.out.println(); } if (HttpHeaders.isTransferEncodingChunked(response)) { System.out.println("CHUNKED CONTENT {"); } else { System.out.println("CONTENT {"); } } if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; System.out.print(content.content().toString(CharsetUtil.UTF_8)); System.out.flush(); if (content instanceof LastHttpContent) { System.out.println("} END OF CONTENT"); } } } @Override public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }
initialization code:
public class NettyClientInitializer extends ChannelInitializer<SocketChannel> { private final boolean ssl; public NettyClientInitializer(boolean ssl) { this.ssl = ssl; } @Override public void initChannel(SocketChannel ch) throws Exception {
Mitaksh gupta
source share