本文整理汇总了Java中io.netty.handler.codec.http.HttpHeaderUtil.setContentLength方法的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaderUtil.setContentLength方法的具体用法?Java HttpHeaderUtil.setContentLength怎么用?Java HttpHeaderUtil.setContentLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.handler.codec.http.HttpHeaderUtil
的用法示例。
在下文中一共展示了HttpHeaderUtil.setContentLength方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: channelRead
import io.netty.handler.codec.http.HttpHeaderUtil; //导入方法依赖的package包/类
@SuppressWarnings("unused")
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
FullHttpRequest request = (FullHttpRequest) msg;
try {
Response httpResp = invoke(parse(request));
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
httpResp.getStatus().getStatus(), httpResp.buffer());
if (httpResp != null) {
HttpHeaderUtil.setContentLength(response, response.content().readableBytes());
HttpHeaderUtil.setKeepAlive(response, true);
ctx.channel().writeAndFlush(response);
} else {
ctx.writeAndFlush(
new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, httpResp.buffer()));
}
} finally {
request.content().release();
}
}
示例2: sendHttpResponse
import io.netty.handler.codec.http.HttpHeaderUtil; //导入方法依赖的package包/类
protected void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200).
if (res.status().code() != 200) {
ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
HttpHeaderUtil.setContentLength(res, res.content().readableBytes());
}
// Send the response and close the connection if necessary.
ChannelFuture f = ctx.channel().writeAndFlush(res);
if (!HttpHeaderUtil.isKeepAlive(req) || res.status().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}