公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

需求点

  1. 实现Get,Post功能
  2. 打印Http请求方法,请求地址,Body内容
  3. 返回数据到前端。

看代码如下

HttpServer

public class HttpServer {
    public void run(int port){
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup,workGroup)                .channel(NIOServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        //设置http解码器
                        ch.pipeline().addLast(new HttpRequestDecoder());
                        //设置http内容处理器
                        ch.pipeline().addLast(new HttpObjectAggregator(65536));
                        //设置http编码器
                        ch.pipeline().addLast(new HttpResponseEncoder());
                        //自定义服务处理器
                        ch.pipeline().addLast(new HttpServerHandler());
                    }
                });
        try {
            ChannelFuture future = bootstrap.bind(8080).sync();
            System.out.println("服务器启动成功");
            future.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
    public static void main(String[] args) {
        new HttpServer().run(8080);
    }
}

自定义网络I/O时间处理器HttpServerHandler

public class HttpServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //判断是不是http请求
        if(msg instanceof HttpRequest){
            HttpRequest httpRequest = (HttpRequest) msg;
            parseUri(httpRequest);
            parseHttpMethod(httpRequest);
            parseHttpHeaders(httpRequest);
            parseBody(httpRequest);
            FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrAppedBuffer("ok".getBytes()));
            HttpUtil.setContentLength(res, res.content().readableBytes());
            ctx.writeAndFlush(res);
        }
        super.channelRead(ctx, msg);
    }
    /**
     * 获得请求方式
     * @param httpRequest
     */
    private HttpMethod parseHttpMethod(HttpRequest httpRequest){
        HttpMethod httpMethod = httpRequest.method();
        System.out.println("method:"+httpMethod.name());
        return httpMethod;
    }
    /**
     * 打印头部信息
     * @param httpRequest
     */
    private HttpHeaders parseHttpHeaders(HttpRequest httpRequest){
        HttpHeaders httpHeaders = httpRequest.headers();
        for (Map.Entry<String, String> entry : httpHeaders.entries()) {
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
        return httpHeaders;
    }
    /**
     * 打印请求地址
     * @param httpRequest
     */
    private void parseUri(HttpRequest httpRequest){
        String uri = httpRequest.uri();
        System.out.println("uri:"+uri);
    }
    /**
     * 打印请求体
     * @param httpRequest
     */
    private void parseBody(HttpRequest httpRequest){
        if(httpRequest instanceof HttpContent){
            HttpContent httpContent = (HttpContent) httpRequest;
            System.out.println("content:"+httpContent.content().toString(Charset.defaultCharset()));
        }
    }
}

启动HttpServer。

网页输入http://localhost:8080/

控制台显示

uri:/
method:GET
Host:localhost:8080
Connection:keep-alive
Cache-Control:max-age=0
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (windows NT 6.1; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/85.0.4183.102 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site:noneSec-Fetch-Mode:navigateSec-Fetch-User:?1
Sec-Fetch-Dest:documentAccept-Encoding:gzip, deflate, brAccept-Language:en,zh-CN;q=0.9,zh;q=0.8
content-length:0
content:

PostMan用Post方式请求http://localhost:8080/

控制台显示如下

uri:/
method:POST
Content-Type:text/plain
User-Agent:PostmanRuntime/7.26.3
Accept:*/*
Cache-Control:no-cache
Postman-Token:dbdb420e-5403-40da-8d8f-e2c11029c390
Host:localhost:8080
Accept-Encoding:gzip, deflate, br
Connection:keep-alive
Content-Length:21
content:{
    "test":1212
}

代码实现起来不难,我们一步步来解析,为什么要这样做。

 

HttpRequestDecoder

首先HttpRequestDecoder是一个解码器,那就是从网络读取字节流来处理。

它的主要功能是,解析ByteBuf转化成Http请求的相关数据,例如将请求数据转化成,HttpRequest/HttpResponse,HttpContent,LastHttpContent。

 

HttpObjectAggregator

HttpObjectAggregator的作用是聚合,因为HttpRequestDecoder会解析成多个Http相关对象,它将它们聚合成一个对象。

HttpResponseEncoder

HttpResponseEncoder的作用是用来编码,将我们响应的Http对象,转化成ByteBuf,进行网络通信。

 

我们最终需要把网络请求的数据转化成http请求的相关类。

我这里用相关类来表示,不特指netty的httpRequest和HttpResponse。因为我们自己也可以自定义实现。只要按照http请求的报文做相应的解析即可。

我们来看下网络请求的过程。

Netty核心10-简易实现Http服务器

 

HttpServerHandler是我们自定义的网络I/O处理事件。当读数据的时候,判断msg是HttpRequest类型,说明是http请求,做相应的处理即可。

小知识点

对于一个陌生的框架,如果想知道它是否有某个功能,我们直接进去看接口有没有相关的实现即可。

不用百度的,直接看接口,观察函数参数,以及返回类型,就可以开始使用。如果调用不通,看注释等等,最后才看下百度。

总结

我们本篇,讲解了一个例子,用netty来做http服务器。其中网络I/O事件打印了,请求的方式(GET/POST),headers,post body,uri。

有了这几个参数,基本上的http相关操作都可以实现。

但是HttpRequestDecoder,HttpObjectAggregator,HttpResponseEncoder是如何实现的呢?

分享到:
标签:服务器 Http
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定