简体   繁体   English

如何为基于Apache HttpComponents的服务器添加Keep-Alive标头?

[英]How to add Keep-Alive header for server based on Apache HttpComponents?

How can I add "Connection: Keep-Alive" and "Keep-Alive: timeout=x, max=y" headers to a response using Apache HttpComponents ONLY if the connection is going to be persistent? 仅在连接要持久的情况下,才如何使用Apache HttpComponents将“ Connection:Keep-Alive”和“ Keep-Alive:timeout = x,max = y”标头添加到响应中?

If HttpComponents decides this connection won't be persistent, it adds a "Connection: close" header after I give the response to it. 如果HttpComponents决定此连接将不会持久,则在响应后添加“ Connection:close”标头。 I don't want a Keep-Alive header in this case. 在这种情况下,我不需要Keep-Alive标头。

Why I'm doing this: 为什么我这样做:

Standard behavior is for HttpComponents to change nothing in the response for a persistent connection, and add "Connection: close" for non-persistent connections. 对于HttpComponents,标准行为是对持久连接的响应不做任何更改,对于非持久连接则添加“ Connection:close”。 This works well in most cases. 在大多数情况下,此方法效果很好。

I want to have a Keep-Alive header because clients based on the standard java.net.HttpURLConnection will time out and throw away connections after 5 seconds of inactivity unless there is a Keep-Alive header in the previous response from the server. 我想要一个Keep-Alive标头,因为基于标准java.net.HttpURLConnection的客户端将在不活动5秒后超时并丢弃连接,除非服务器先前的响应中没有Keep-Alive标头。 I want to use Keep-Alive to define a longer timeout than 5 seconds. 我想使用Keep-Alive定义比5秒更长的超时时间。

You can add HttpResponseInterceptor, which adds "Connection: Keep-Alive" and "Keep-Alive: timeout=x, max=y" headers to a response depending on the evaluation from org.apache.http.protocol.ResponseConnControl, which set "Connection: close" header, when needed. 您可以根据响应来自org.apache.http.protocol.ResponseConnControl的评估将HttpResponseInterceptor添加到响应中,以添加“ Connection:Keep-Alive”和“ Keep-Alive:timeout = x,max = y”标头,连接:在需要时关闭“”标题。

class ResposeKeepAliveHeaderMod implements HttpResponseInterceptor {

    @Override
    public void process(HttpResponse response, HttpContext context)
            throws HttpException, IOException {
        final Header explicit = response.getFirstHeader(HTTP.CONN_DIRECTIVE);
        if (explicit != null && HTTP.CONN_CLOSE.equalsIgnoreCase(explicit.getValue())) {
            // Connection persistence explicitly disabled
            return;
        }else{
            // "Connection: Keep-Alive" and "Keep-Alive: timeout=x, max=y" 
            response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
            response.setHeader(HTTP.CONN_KEEP_ALIVE, "timeout=30, max=100");
        }

    }      
   }

You need to add this to HttpProcessor, after ResponseConnControl: 您需要在ResponseConnControl之后将其添加到HttpProcessor中:

HttpProcessor httpProcessor = HttpProcessorBuilder.create()
            //.addFirst(new RequestTrace())
            .add(new ResponseDate())
            //.add(new ResponseServer("MyServer-HTTP/1.1"))
            .add(new ResponseContent())
            .add(new ResponseConnControl())
            .addLast(new ResposeKeepAliveHeaderMod())
            .build();

Then build the server: 然后构建服务器:

final HttpServer server = ServerBootstrap.bootstrap()
            .setListenerPort(9090)
            .setHttpProcessor(httpProcessor)
            .setSocketConfig(socketConfig)
            .setExceptionLogger(new StdErrorExceptionLogger ())
            .setHandlerMapper(handle_map)
            .create();

I haven't tried this, but it seems you can code a custom 'ConnectionKeepAliveStrategy' for your httpCleint. 我没有尝试过,但是看来您可以为您的httpCleint编写自定义的“ ConnectionKeepAliveStrategy”。 Here is documentation HttpComponents Custom stratagey 这是文档HttpComponents自定义策略

Refer section 2.11 请参阅第2.11节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM