0

我需要使用 Netty 创建一个服务器应用程序,它将接收像“GETs”或“POSTs”这样的请求。在 GET 请求的情况下,参数将作为查询参数出现。

我一直在检查 HttpRequestDecoder 是否适合 GET 请求,以及 HttpPostRequestDecoder 是否适合帖子。但是我怎么能同时处理这两个呢?

对Netty不是很熟悉,所以我会感谢一点帮助:)

4

2 回答 2

2

netty 规定我们将请求作为管道处理,您将管道定义为一系列处理程序。

一个序列可能是这样的:

p.addLast ("codec", new HttpServerCodec ());
p.addLast ("handler", new YourHandler());

其中 p 是 ChannelPipeline 接口的一个实例。您可以按如下方式定义 YourHandler 类:

public class YourHandler extends ChannelInboundHandlerAdapter
{
    @Override
    public void channelRead (ChannelHandlerContext channelHandlerCtxt, Object msg)
        throws Exception
    {
        // Handle requests as switch cases. GET, POST,...
        // This post helps you to understanding switch case usage on strings:
        // http://stackoverflow.com/questions/338206/switch-statement-with-strings-in-java
        if (msg instanceof FullHttpRequest)
        {
            FullHttpRequest fullHttpRequest = (FullHttpRequest) msg;
            switch (fullHttpRequest.getMethod ().toString ())
            {
                case "GET":
                case "POST":
                ...
            }
        }
    }
}
于 2013-10-17T17:58:58.943 回答
0

您想首先检查请求类型并打开值(GET/POST/PUT/DELETE 等...)

http://docs.jboss.org/netty/3.1/api/org/jboss/netty/handler/codec/http/HttpMethod.html

于 2013-10-17T11:13:09.527 回答