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();
		}

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用Netty框架来实现后台的实时传送到台。Netty是高性能的网络编框架,提供异步事件驱动的应用程序开发模型。 以下是简单的示例代码,展示了何使用Netty实现后台数据的时传送到前台: ```java import io.netty.bootstrap.ServerBootstrap; import io.channel.ChannelFuture; import.netty.channel.ChannelInitializer; import.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.handler.stream.ChunkedWriteHandler; public class Server { private int port; public Server(int port) { this.port = port; } public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( new StringDecoder(), new StringEncoder(), new ChunkedWriteHandler(), new ServerHandler()); } }) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; new Server(port).run(); } } ``` 在这个示例中,我们创建了一个Netty服务器,并且指定了端口号为8080。在`ChannelInitializer`的`initChannel`方法中,我们添加了一系列的处理器,包括`StringDecoder`和`StringEncoder`用于处理字符串消息的编解码,`ChunkedWriteHandler`用于处理大数据流的传输,以及自定义的`ServerHandler`用于处理具体的业务逻辑。 你可以根据自己的需求,在`ServerHandler`中实现数据传送的逻辑。例如,可以在收到新数据时将数据发送到前台。 需要注意的是,这只是一个简单的示例代码,实际使用时可能需要根据具体的业务需求进行适当的修改和扩展。 希望这个示例能对你有所帮助!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值