Here's my situation:
- Client sends a POST request to Netty Server.
- Netty processes the POST request and If the server determines that a response needs to be sent It sends a response back to the client. Else The server has to send a POST request to another end point, get the response and send that response back to the client.
So far I have got the incoming POST request working. To send the outgoing POST request this is what I'm doing in my Handler.
private void sendHttpPost(String input, ChannelHandlerContext ctx) {
try {
String url = "http://localhost";
URI uri = new URI(url);
Bootstrap b = new Bootstrap();
b.group(new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.handler(new PostRequestHandler())
.option(ChannelOption.AUTO_READ, false);
Channel f = b.connect("REMOTE_HOST", 8888).sync().channel();
HttpRequest postReq = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.POST, uri.getRawPath());
postReq.headers().set(HttpHeaders.Names.HOST, "localhost");
postReq.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
postReq.headers().set(HttpHeaders.Names.CONTENT_TYPE,"application/x-www-form-urlencoded");
f.writeAndFlush(postReq);
// Wait for the server to close the connection.
f.closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}
}
This is obviously wrong because I'm starting a new Bootstrap process within the handler. However, I can't send a HttpRequest without a channel. I can't reuse the existing Channel in the ChannelHandlerContext.
What's the correct way to do this ? How can I fork a new HttpPostRequest. Any help will be greatly appreciated.
"REMOTE_HOST"is wrong i guess..? - Sudheera