本文整理匯總了Java中io.netty.handler.codec.http.DefaultFullHttpResponse.headers方法的典型用法代碼示例。如果您正苦於以下問題:Java DefaultFullHttpResponse.headers方法的具體用法?Java DefaultFullHttpResponse.headers怎麽用?Java DefaultFullHttpResponse.headers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io.netty.handler.codec.http.DefaultFullHttpResponse
的用法示例。
在下文中一共展示了DefaultFullHttpResponse.headers方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: directHandleMethod
import io.netty.handler.codec.http.DefaultFullHttpResponse; //導入方法依賴的package包/類
private boolean directHandleMethod(ChannelHandlerContext ctx, FullHttpRequest request, HttpMethod method) {
if (method.equals(HttpMethod.GET) || method.equals(HttpMethod.POST)) {
return false;
}
// 處理 OPTIONS 請求
HttpResponseStatus status = HttpResponseStatus.OK;
boolean invalid = false;
if (!method.equals(HttpMethod.OPTIONS)) {
invalid = true;
status = HttpResponseStatus.METHOD_NOT_ALLOWED;
}
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.EMPTY_BUFFER);
HttpHeaders headers = response.headers();
// headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS,
// "X-Requested-With, accept, origin, content-type");
headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "X-Requested-With, content-type");
headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "GET,POST,OPTIONS");
headers.set(HttpHeaderNames.SERVER, "Netty5");
if (invalid) {
headers.set("Client-Warning", "Invalid Method");
}
boolean keepAlive = HttpHeaderUtil.isKeepAlive(request);
if (keepAlive) {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
ctx.write(response);
ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
if (!keepAlive) {
future.addListener(ChannelFutureListener.CLOSE);
}
return true;
}
示例2: wrapMessage
import io.netty.handler.codec.http.DefaultFullHttpResponse; //導入方法依賴的package包/類
@Override
public Object wrapMessage(ChannelHandlerContext ctx, ThriftMessage msg) {
HttpResponseStatus status = HttpResponseStatus.OK;
DefaultFullHttpResponse httpResp = new DefaultFullHttpResponse(HTTP_1_1, status, msg.getContent());
HttpHeaders headers = httpResp.headers();
headers.set(HttpHeaderNames.CONTENT_TYPE, "application/json");
headers.set(HttpHeaderNames.SERVER, "Netty5");
filterResponse(httpResp, msg);
return httpResp;
}