在浏览器中取消页面加载时,服务器中会发生什么?

时间:2021-08-21 06:40:24

In a fictitious web application ...

在一个虚构的Web应用程序中......

  1. The user clicks a link
  2. 用户单击链接

  3. The server starts to prepare the response, but it takes several seconds
  4. 服务器开始准备响应,但需要几秒钟

  5. The user cancels the page load
  6. 用户取消页面加载

What happens to the request? Does the server continue to prepare the response? Does the response arrive to the browser?

请求会发生什么?服务器是否继续准备响应?响应是否到达浏览器?

4 个解决方案

#1


5  

The server will continue to prepare the response. When it tries to send the response to the client, it'll fail. When this actually happens will probably depend on the actual application server implementation, whether the response is buffered etc.

服务器将继续准备响应。当它尝试将响应发送到客户端时,它将失败。实际发生时,可能取决于实际的应用程序服务器实现,响应是否缓冲等。

In Java EE app servers (Tomcat and WebLogic, probably others as well), you'll get the following exception:

在Java EE应用服务器(Tomcat和WebLogic,可能还有其他服务器)中,您将获得以下异常:

java.net.SocketException: Connection reset by peer: socket write error

#2


1  

PHP understands three states of connection: NORMAL, ABORTED and TIMEOUT. You can change PHP's policy on ABORTED connections (by default, the script is terminated) with the ignore_user_abort() function. From the notes section:

PHP了解三种连接状态:NORMAL,ABORTED和TIMEOUT。您可以使用ignore_user_abort()函数更改ABORTED连接上的PHP策略(默认情况下,脚本已终止)。从笔记部分:

"PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client."

“在尝试向客户端发送信息之前,PHP不会检测到用户已中止连接。”

Note that if your server's output is buffered, a send may not occur immediately.

请注意,如果缓冲了服务器的输出,则可能不会立即发送。

See PHP's page on connection handling for more details.

有关更多详细信息,请参阅PHP的连接处理页面。

#3


1  

If the connection was broken before the request was completely sent to the server, the response processing won't occur.

如果在请求完全发送到服务器之前断开连接,则不会发生响应处理。

If the request was sent completely, this triggers the server-side processing, and the response generation will continue despite the broken connection.

如果请求是完全发送的,则会触发服务器端处理,尽管连接断开,响应生成仍将继续。

In ASP.NET, you can detect this by using Response.IsClientConnected to halt the processing if the client is not connected anymore, saving CPU time and returning the thread immediately to the thread pool.

在ASP.NET中,您可以通过使用Response.IsClientConnected来检测此情况,如果客户端不再连接则停止处理,节省CPU时间并立即将线程返回到线程池。

#4


0  

I would think that the actual TCP connection is closed by the browser and therefore the web-server will be unable to send data, and unless it is specifically programmed to detect broken connections whilst preparing the data, then the page will be fully processed even if the user cancels.

我认为实际的TCP连接被浏览器关闭,因此Web服务器将无法发送数据,除非专门编程以在准备数据时检测断开的连接,否则页面将被完全处理,即使用户取消。

I have little knowledge on these things though, but that would be my guess.

我对这些事情知之甚少,但那是我的猜测。

#1


5  

The server will continue to prepare the response. When it tries to send the response to the client, it'll fail. When this actually happens will probably depend on the actual application server implementation, whether the response is buffered etc.

服务器将继续准备响应。当它尝试将响应发送到客户端时,它将失败。实际发生时,可能取决于实际的应用程序服务器实现,响应是否缓冲等。

In Java EE app servers (Tomcat and WebLogic, probably others as well), you'll get the following exception:

在Java EE应用服务器(Tomcat和WebLogic,可能还有其他服务器)中,您将获得以下异常:

java.net.SocketException: Connection reset by peer: socket write error

#2


1  

PHP understands three states of connection: NORMAL, ABORTED and TIMEOUT. You can change PHP's policy on ABORTED connections (by default, the script is terminated) with the ignore_user_abort() function. From the notes section:

PHP了解三种连接状态:NORMAL,ABORTED和TIMEOUT。您可以使用ignore_user_abort()函数更改ABORTED连接上的PHP策略(默认情况下,脚本已终止)。从笔记部分:

"PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client."

“在尝试向客户端发送信息之前,PHP不会检测到用户已中止连接。”

Note that if your server's output is buffered, a send may not occur immediately.

请注意,如果缓冲了服务器的输出,则可能不会立即发送。

See PHP's page on connection handling for more details.

有关更多详细信息,请参阅PHP的连接处理页面。

#3


1  

If the connection was broken before the request was completely sent to the server, the response processing won't occur.

如果在请求完全发送到服务器之前断开连接,则不会发生响应处理。

If the request was sent completely, this triggers the server-side processing, and the response generation will continue despite the broken connection.

如果请求是完全发送的,则会触发服务器端处理,尽管连接断开,响应生成仍将继续。

In ASP.NET, you can detect this by using Response.IsClientConnected to halt the processing if the client is not connected anymore, saving CPU time and returning the thread immediately to the thread pool.

在ASP.NET中,您可以通过使用Response.IsClientConnected来检测此情况,如果客户端不再连接则停止处理,节省CPU时间并立即将线程返回到线程池。

#4


0  

I would think that the actual TCP connection is closed by the browser and therefore the web-server will be unable to send data, and unless it is specifically programmed to detect broken connections whilst preparing the data, then the page will be fully processed even if the user cancels.

我认为实际的TCP连接被浏览器关闭,因此Web服务器将无法发送数据,除非专门编程以在准备数据时检测断开的连接,否则页面将被完全处理,即使用户取消。

I have little knowledge on these things though, but that would be my guess.

我对这些事情知之甚少,但那是我的猜测。