语言

ServicePoint.MaxIdleTime 属性

定义

获取或设置与 ServicePoint 对象关联的连接在连接关闭之前可以保持空闲的时间量。

public:
 property int MaxIdleTime { int get(); void set(int value); };
public int MaxIdleTime { get; set; }
member this.MaxIdleTime : int with get, set
Public Property MaxIdleTime As Integer

属性值

与 ServicePoint 对象关联的连接在关闭并重新用于另一个连接之前可以保持空闲的时间长度(以毫秒为单位)。

例外

示例

下面的代码示例使用 MaxIdleTime 属性来设置和检索 ServicePoint 空闲时间。

// Display the date and time that the ServicePoint was last
// connected to a host.
Console.WriteLine("IdleSince = " + sp.IdleSince.ToString());

// Display the maximum length of time that the ServicePoint instance
// is allowed to maintain an idle connection to an Internet
// resource before it is recycled for use in another connection.
Console.WriteLine("MaxIdleTime = " + sp.MaxIdleTime);
' Display the date and time that the ServicePoint was last 
' connected to a host.
Console.WriteLine(("IdleSince = " + sp.IdleSince.ToString()))


' Display the maximum length of time that the ServicePoint instance 
' is allowed to maintain an idle connection to an Internet  
' resource before it is recycled for use in another connection.
Console.WriteLine(("MaxIdleTime = " + sp.MaxIdleTime.ToString()))

注解

Caution

WebRequest、 HttpWebRequest、 ServicePoint和 WebClient 已过时,不应将其用于新开发。 改用 HttpClient。

可以设置为MaxIdleTimeTimeout.Infinite指示与ServicePoint对象关联的连接不应超时。

属性的MaxIdleTime默认值是创建对象时ServicePointManager.MaxServicePointIdleTime属性的值ServicePoint。 对属性的后续更改 MaxServicePointIdleTime 对现有 ServicePoint 对象没有任何影响。

MaxIdleTime当超出与某个ServicePoint连接关联的连接时,连接将保持打开状态,直到应用程序尝试使用该连接。 此时,框架将关闭连接,并创建与远程主机的新连接。

注释

自 .NET 9 起,此属性映射到 SocketsHttpHandler.PooledConnectionIdleTimeout. 但是,处理程序不会在请求之间重复使用,因此它没有任何有意义的影响。

适用于