有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    根据你所说的,我认为this page会给你你需要的答案。最中肯的话是:

    If the Keep-Alive header is not present in the response, HttpClient assumes the connection can be kept alive indefinitely. However, many HTTP servers in general use are configured to drop persistent connections after a certain period of inactivity in order to conserve system resources, quite often without informing the client.

    所以,基本上,这取决于服务器的合作程度。不过,归根结底,请记住,keep-alive值是对服务器的一个建议,您不能保证它会得到遵守。这可能是因为服务器试图从非活动连接中回收有限的资源,或者底层TCP连接出现问题,或者只是编程不好,所以最好制定一个策略,在出现故障时重新连接

  2. # 2 楼答案

    如果在一段时间内没有真正的请求,您可以在循环中执行一个假“ping”请求,以尽可能长时间地保持连接。然而,这会给服务器带来不必要的负载,应用程序的性能增益将是最小的