本文整理汇总了Java中io.netty.handler.codec.http.HttpHeaders.setKeepAlive方法的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaders.setKeepAlive方法的具体用法?Java HttpHeaders.setKeepAlive怎么用?Java HttpHeaders.setKeepAlive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.handler.codec.http.HttpHeaders
的用法示例。
在下文中一共展示了HttpHeaders.setKeepAlive方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeBack
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
public static int writeBack(Channel channel, boolean isSuccess, String resultStr, boolean isKeepAlive) {
ByteBuf content = Unpooled.copiedBuffer(resultStr, Constants.DEFAULT_CHARSET);
HttpResponseStatus status;
if (isSuccess) {
status = HttpResponseStatus.OK;
} else {
status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
}
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, status, content);
//logger.info("result str:{}", resultStr);
res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
HttpHeaders.setContentLength(res, content.readableBytes());
try {
ChannelFuture f = channel.writeAndFlush(res);
if (isKeepAlive) {
HttpHeaders.setKeepAlive(res, true);
} else {
HttpHeaders.setKeepAlive(res, false);//set keepalive closed
f.addListener(ChannelFutureListener.CLOSE);
}
} catch (Exception e2) {
logger.warn("Failed to send HTTP response to remote, cause by:", e2);
}
return content.readableBytes();
}
示例2: handleNonProxyRequest
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
protected HttpResponse handleNonProxyRequest(FullHttpRequest req) {
String uri = req.getUri();
if ("/version".equals(uri)) {
if (HttpMethod.GET.equals(req.getMethod())) {
JsonObject jsonObj = new JsonObject();
jsonObj.addProperty("name", m_appConfig.getAppName());
jsonObj.addProperty("version", m_appConfig.getAppVersion());
byte[] content = jsonObj.toString().getBytes(CharsetUtil.UTF_8);
DefaultFullHttpResponse resp = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
Unpooled.copiedBuffer(content));
HttpHeaders.setKeepAlive(resp, false);
HttpHeaders.setHeader(resp, HttpHeaders.Names.CONTENT_TYPE,
"application/json");
HttpHeaders.setContentLength(resp, content.length);
return resp;
}
}
return RESPONSE_404;
}
示例3: respondWithShortCircuitResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Responds to the client with the specified "short-circuit" response. The response will be sent through the
* {@link HttpFilters#proxyToClientResponse(HttpObject)} filter method before writing it to the client. The client
* will not be disconnected, unless the response includes a "Connection: close" header, or the filter returns
* a null HttpResponse (in which case no response will be written to the client and the connection will be
* disconnected immediately). If the response is not a Bad Gateway or Gateway Timeout response, the response's headers
* will be modified to reflect proxying, including adding a Via header, Date header, etc.
*
* @param httpResponse the response to return to the client
* @return true if the connection will be kept open, or false if it will be disconnected.
*/
private boolean respondWithShortCircuitResponse(HttpResponse httpResponse) {
// we are sending a response to the client, so we are done handling this request
this.currentRequest = null;
HttpResponse filteredResponse = (HttpResponse) currentFilters.proxyToClientResponse(httpResponse);
if (filteredResponse == null) {
disconnect();
return false;
}
// allow short-circuit messages to close the connection. normally the Connection header would be stripped when modifying
// the message for proxying, so save the keep-alive status before the modifications are made.
boolean isKeepAlive = HttpHeaders.isKeepAlive(httpResponse);
// if the response is not a Bad Gateway or Gateway Timeout, modify the headers "as if" the short-circuit response were proxied
int statusCode = httpResponse.getStatus().code();
if (statusCode != HttpResponseStatus.BAD_GATEWAY.code() && statusCode != HttpResponseStatus.GATEWAY_TIMEOUT.code()) {
modifyResponseHeadersToReflectProxying(httpResponse);
}
// restore the keep alive status, if it was overwritten when modifying headers for proxying
HttpHeaders.setKeepAlive(httpResponse, isKeepAlive);
write(httpResponse);
if (ProxyUtils.isLastChunk(httpResponse)) {
writeEmptyBuffer();
}
if (!HttpHeaders.isKeepAlive(httpResponse)) {
disconnect();
return false;
}
return true;
}