下面列出了 io.netty.handler.codec.http.HttpResponseStatus # FOUND 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void writeResponseLocation(Channel channel, String URL) {
// Decide whether to close the connection or not.
boolean close = false;
// Build the response object.
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.FOUND);
response.headers().set(CONTENT_TYPE, ContentType.HTML);
response.headers().set(LOCATION, URL);
response.headers().set(CONTENT_LENGTH, 0);
// Write the response.
ChannelFuture future = channel.writeAndFlush(response);
future.addListener(ChannelFutureListener.CLOSE);
}
@Override
public FullHttpResponse respond(FullHttpRequest req, ChannelHandlerContext ctx) throws Exception {
doAction(req, ctx);
DefaultFullHttpResponse res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FOUND);
res.headers().set(HttpHeaders.Names.LOCATION, redirectUri);
return res;
}
public void sendRedirect(ChannelHandlerContext ctx, String newUri) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FOUND);
response.headers().set(HttpHeaders.Names.LOCATION, newUri);
// Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
private FullHttpResponse redirect(QueryStringEncoder encoder) {
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FOUND);
response.headers().add(HttpHeaders.LOCATION, encoder.toString());
return response;
}