當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。