小编Jet*_*Jet的帖子

如何使用netty编写http代理

我想使用netty编写一个简单的程序来代理浏览器发送的http请求。我觉得可以分为3个步骤

  1. 获取浏览器发送的请求
  2. 发送到网站
  3. 从网站接收数据并将其发送回浏览器。

题:

  1. 使用 Bootstrap.connect(host, port) 时如何将 url 转换为主机和端口;
  2. 当我使用 HttpServerResponseHandler.connect 和 ChannelHandlerContext.writeAndFlush(httpMessage); 要将数据发送到网站,如何从网站获取响应数据并将其发送回浏览器?

这是我学习netty的第一天,所以请尽量回答简单。非常感谢。

public class Server {
    public static void main(String[] args) throws InterruptedException {
        final int port = 8888;

        // copy from https://github.com/netty/netty/wiki/User-guide-for-4.x
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new HttpRequestDecoder(), new HttpServerRequestHandler());
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            // …
Run Code Online (Sandbox Code Playgroud)

java proxy netty

6
推荐指数
1
解决办法
7035
查看次数

标签 统计

java ×1

netty ×1

proxy ×1