当前位置: 首页>>代码示例>>Java>>正文


Java HttpHeaders.setKeepAlive方法代码示例

本文整理汇总了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();
}
 
开发者ID:tiglabs,项目名称:jsf-sdk,代码行数:27,代码来源:HttpJsonHandler.java

示例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;
}
 
开发者ID:eBay,项目名称:ServiceCOLDCache,代码行数:22,代码来源:NettyRequestProxyFilter.java

示例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;
}
 
开发者ID:wxyzZ,项目名称:little_mitm,代码行数:48,代码来源:ClientToProxyConnection.java


注:本文中的io.netty.handler.codec.http.HttpHeaders.setKeepAlive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。