如何使用HttpClient 4.3设置TCP_NODELAY和SO_KEEPALIVE

时间:2014-09-13 09:15:18

标签: apache-httpclient-4.x

使用Apache HttpClient 4.3如何在不使用已弃用的HttpParams的情况下关闭Nagle算法(TCP_NODELAY)并打开TCP保持活动数据包(SO_KEEPALIVE)?

注意:TCP keep-alive和HTTP keep-alive是两回事。

1 个答案:

答案 0 :(得分:5)

SocketConfig socketConfig = SocketConfig.custom()
        .setSoKeepAlive(true)
        .setTcpNoDelay(true)
        .build();
CloseableHttpClient client = HttpClients.custom()
        .setDefaultSocketConfig(socketConfig)
        .build();