Netty HttpResponseStatus

Netty HttpResponseStatus

private HttpResponseStatus(int code, String reasonPhrase, boolean bytes)
{
    if(code < 0)
        throw new IllegalArgumentException((new StringBuilder()).append("code: ").append(code).append(" (expected: 0+)").toString());
    if(reasonPhrase == null)
        throw new NullPointerException("reasonPhrase");
    int i = 0;
    do
    {
        if(i >= reasonPhrase.length())
            break;
        char c = reasonPhrase.charAt(i);
        switch(c)
        {
        case 10: // '\n'
        case 13: // '\r'
            throw new IllegalArgumentException((new StringBuilder()).append("reasonPhrase contains one of the following prohibited characters: \r\n: ").append(reasonPhrase).toString());
        }
        i++;
    } while(true);
    this.code = code;
    codeAsText = new AsciiString(Integer.toString(code));
    this.reasonPhrase = reasonPhrase;
    if(bytes)
        this.bytes = (new StringBuilder()).append(code).append(" ").append(reasonPhrase).toString().getBytes(CharsetUtil.US_ASCII);
    else
        this.bytes = null;
}

此代码是 "io.netty.handler.codec.http.HttpResponseStatus" 的一部分。

我不知道这个值是什么,"String reasonPhrase"是什么意思。

我给什么值"reasonPhrase"?

我的Netty版本是4.1.19.Final.

原因短语是 HTTP 响应中状态行的一部分。例如,在200 OK中,OK是原因短语。

您几乎不需要提供您自己的原因短语(或者实际上构建您自己的 HttpResponseStatus)。最常见的响应状态在 HttpResponseStatus class 中定义为常量。例如,要创建带有正文的 200 OK 的 HTTP 响应,代码如下所示:

FullHttpResponse response = new DefaultFullHttpResponse(
            HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);

如果您有代码,但想要 HTTP 状态代码,请改为使用静态方法从整数生成 HTTP 响应状态对象。

https://netty.io/4.1/api/io/netty/handler/codec/http/HttpResponseStatus.html#valueOf-int-