Java 使用 Netty DefaultFullHttpRequest 实现Post发送数据包的功能

3 篇文章 0 订阅
		// 向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();
		}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值