// 向SQLMAPAPI传送开始测试的指令
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new HttpRequestEncoder());// 客户端对发送的httpRequest进行编码
socketChannel.pipeline().addLast(new HttpResponseDecoder());// 客户端需要对服务端返回的httpresopnse解码
socketChannel.pipeline().addLast(new StartTestResponse());
}
});
Channel channel = bootstrap.connect("127.0.0.1", 8775).sync().channel();
// *** 生成post传送的uri
URI uri = new URI("/scan/" + taskid + "/start");
// *** 设置POST数据包中传输的数据 ***
String content = "hello post";
FullHttpRequest requestToSQLMAPAPI = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
uri.toASCIIString(), Unpooled.wrappedBuffer(content.getBytes("UTF-8")));
requestToSQLMAPAPI.headers().set(HttpHeaders.Names.HOST, "127.0.0.1");
requestToSQLMAPAPI.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
requestToSQLMAPAPI.headers().set(HttpHeaders.Names.CONTENT_LENGTH,
requestToSQLMAPAPI.content().readableBytes());
requestToSQLMAPAPI.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
// headers.set("Host", "127.0.0.1");
// headers.set("Connection", HttpHeaderValues.CLOSE);
// headers.set("Content-Type", "application/json");
// headers.set("Content-Length", "" + contentByteBuf.capacity());
// headers.set("User-Agent", "Python-urllib/2.7");
// headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP + "," +
// HttpHeaderValues.DEFLATE);
// headers.set(HttpHeaderNames.ACCEPT_CHARSET,
// "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
// headers.set(HttpHeaderNames.ACCEPT_LANGUAGE, "fr");
// headers.set(HttpHeaderNames.USER_AGENT, "Netty Simple Http Client side");
// headers.set(HttpHeaderNames.ACCEPT,
// "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
// headers.set(HttpHeaderNames.HOST, "127.0.0.1:8775");
// headers.set(HttpHeaderNames.CONTENT_TYPE, "application/json");
// headers.set(HttpHeaderNames.CONNECTION, "close");
// headers.set(HttpHeaderNames.USER_AGENT, "Python-urllib/2.7");
// *
// *** 输出requestToSQLMAPAPI的内容
System.out.println("---requestToSQLMAPAPI---");
System.out.println(requestToSQLMAPAPI.toString());
System.out.println();
System.out.println(requestToSQLMAPAPI.content().toString(0, requestToSQLMAPAPI.content().capacity(),
Charset.defaultCharset())); //
// */
// send request
channel.writeAndFlush(requestToSQLMAPAPI).sync();
channel.closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
}