简体   繁体   English

为什么仅在使用WebRequest的第一个请求上发送连接保持活动状态?

[英]Why is connection keep-alive only sent on first request with WebRequest?

I have the following code for testing purposes 我有以下代码用于测试目的

for( int i = 0; i < 5 ; i++ ) {   
   var url = "http://myserver.com/Warmup";
   var request = WebRequest.Create(url);
   using (WebResponse response = request.GetResponse()) {
       using (var stream = response.GetResponseStream()) {
           using (var reader = new StreamReader(stream)) {
               reader.ReadToEnd();
           }
       }
   }
}

When I run it with Fiddler I see that only the first request has Connection: Keep-Alive header. 当我用Fiddler运行它时,我看到只有第一个请求具有Connection: Keep-Alive标头。

This means that if the server is IIS programmed to shutdown the application pool after some time of inactivity and that pool shutdown happens after the first request but before the next one then the next request that comes from the client will not have Connection: Keep-Alive set and so keep-alive will not kick in. 这意味着,如果服务器经过IIS编程以在一段时间不活动之后关闭应用程序池,并且该池关闭发生在第一个请求之后但下一个请求之前,则来自客户端的下一个请求将没有Connection: Keep-Alive设置,因此保持活动将不会生效。

Why is Connection: Keep-Alive only sent for the first request and not for the later requests? 为什么“ Connection: Keep-Alive仅发送给第一个请求,而不发送给以后的请求?

HTTP/1.1 uses keep-alive connections by default. HTTP / 1.1默认情况下使用保持活动连接。 A client or server must explicitly indicate that they don't want Keep-Alive behavior by sending a Connection: close header. 客户端或服务器必须通过发送Connection: close标头来明确指示他们不希望保持活动状态。

The Connection: Keep-Alive header is sent on the first request only because the client doesn't yet know whether the server supports HTTP/1.1 or if it will return a HTTP/1.0 response. 仅在第一个请求上发送Connection: Keep-Alive标头,因为客户端尚不知道服务器是否支持HTTP/1.1或是否将返回HTTP/1.0响应。 After the server confirms that it supports HTTP/1.1 by sending a response using that version, the client knows that it can safely drop the redundant header. 服务器通过使用该版本发送响应来确认其支持HTTP/1.1 ,客户端便知道可以安全地删除冗余头。

I think it is because you are using a TCP connection for the request. 我认为这是因为您正在使用TCP连接进行请求。 Just as mentioned here the server creates one TCP connection which is in charge of all the requests you are performing. 就像这里提到的,服务器创建一个TCP连接,该TCP连接负责您正在执行的所有请求。

Since TCP uses a three way handshake the server and client always know if the connection is still available or not. 由于TCP使用三向握手,因此服务器和客户端始终知道连接是否仍然可用。 So everytime the server closes a connection it will send a so called FIN bit to the client so that the client knows that the connection is not established anymore. 因此,每当服务器关闭连接时,它将向客户端发送一个所谓的FIN位,以便客户端知道不再建立连接。

Although you create the webrequest 5 times, the client already knows the request because it is available in its TCP/IP pool and can reuse the connection, unless the client demands to end the connection finally. 尽管您创建了5次webrequest,但客户端已经知道该请求,因为该请求在其TCP / IP池中可用,并且可以重新使用该连接,除非客户端最终要求终止连接。

You can find more information in this wikipedia article about this topic. 您可以在此Wikipedia文章中找到有关此主题的更多信息。

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

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