public class HttpClient {
private final URI uri;
private final String ip;
private final int port;
public void run() {
System.out.println("In run method of HttpClient!!!!!!!!!!!!"+uri);
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
String host = uri.getHost() == null? "localhost" : uri.getHost();
System.out.println("--------------------A------------------------------");
if (!"http".equalsIgnoreCase(scheme) ) {
System.err.println("Only HTTP is supported.");
return;
}
System.out.println("---------------------B----------------------------------");
// Configure the client.
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new HttpClientInitializer());
// Make the connection attempt.
Channel ch = b.connect(host, port).sync().channel();
// StringBuilder fileData = new StringBuilder("xmlToBePushed");
ByteBuf reqContent=Unpooled.copiedBuffer("HELLOOOOOOOOOOOOOOOOOOOOO",CharsetUtil.US_ASCII);
System.out.println(host+"::"+port+"-->Sending dprules xml "+reqContent.toString());
// Prepare the HTTP request.
HttpRequest request = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.POST, uri.getRawPath(),reqContent);
request.headers().set(HttpHeaders.Names.HOST, host);
request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
request.headers().set("IP", this.ip+":"+port);
// Send the HTTP request.
ch.writeAndFlush(request);
// ch.write(request);
// Wait for the server to close the connection.
ch.closeFuture().sync();
} catch (Exception ex){
System.out.println("EXCEPTION!!!!!!!!!!!!!!!!!!"+ex.getMessage());
} finally {
// Shut down executor threads to exit
System.out.println("Client is shuttingdown gracefully..");
group.shutdownGracefully();
}
}
}
服务器服务器处理程序为:
public class HnrServerHandler extends SimpleChannelInboundHandler