HttpResponse.IsClientConnected 属性
定义
获取一个值,通过该值指示客户端是否仍连接在服务器上。Gets a value indicating whether the client is still connected to the server.
public:
property bool IsClientConnected { bool get(); };
public bool IsClientConnected { get; }
member this.IsClientConnected : bool
Public ReadOnly Property IsClientConnected As Boolean
属性值
如果客户端当前仍在连接,则为 true
;否则为 false
。true
if the client is currently connected; otherwise, false
.
示例
下面的示例使用 IsClientConnected 属性来检查请求该页的客户端是否仍连接到服务器。The following example uses the IsClientConnected property to check whether the client that is requesting the page remains connected to the server. 如果 IsClientConnected 为 true,则代码将调用 Redirect 方法,并且客户端将查看另一页。If IsClientConnected is true, the code calls the Redirect method, and the client will view another page. 如果 IsClientConnected 为 false,则代码调用 End 方法并且终止所有页面处理。If IsClientConnected is false, then the code calls the End method and all page processing is terminated.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private void Page_Load(object sender, EventArgs e)
{
// Check whether the browser remains
// connected to the server.
if (Response.IsClientConnected)
{
// If still connected, redirect
// to another page.
Response.Redirect("Page2CS.aspx", false);
}
else
{
// If the browser is not connected
// stop all response processing.
Response.End();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Private Sub Page_Load(sender As Object, e As EventArgs)
' Check whether the browser remains
' connected to the server.
If (Response.IsClientConnected) Then
' If still connected, redirect
' to another page.
Response.Redirect("Page2VB.aspx", false)
Else
' If the browser is not connected
' stop all response processing.
Response.End()
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
注解
IsClientConnected false
当满足以下条件时,属性将返回:The IsClientConnected property returns false
when the following conditions are true:
与客户端的连接已终止。The connection to the client was terminated. 如果 Close 调用了方法,或者客户端停止了 Web 页的执行或浏览到其他页,则会发生这种情况。This can occur if the Close method was invoked, or if the client stopped execution of the Web page or browsed to another page.
HttpWorkerRequest处理请求的对象是
null
或 HttpWorkerRequest.IsClientConnected 方法返回false
。The HttpWorkerRequest object that is handling the request isnull
or the HttpWorkerRequest.IsClientConnected method returnsfalse
. 如果自定义 HttpWorkerRequest 对象处理请求,则 HttpWorkerRequest.IsClientConnected 可能会根据自定义条件设置方法。If a custom HttpWorkerRequest object handles the request, then the HttpWorkerRequest.IsClientConnected method might be set based on custom criteria. 例如,自定义工作请求可能会在一段时间后强制超时。For example, the custom worker request might force a time-out after a period of time.