HttpClient
HttpClient 类
Bugs and Documentation Errors in .NET's HttpClient Frustrate Developers
C# – Configuring HttpClient connection keep-alive
Should I pass the full URL or just the domain to ServicePointManager.FindServicePoint()?
Use HttpClientFactory from .NET 4.6.2
Class
Setting | Default | .NET Framework | .NET Core |
---|---|---|---|
Idle connection timeout | 2 mins | ServicePoint.MaxIdleTime | SocketsHttpHandler.PooledIdleConnectionTimeout |
Max connection lifetime | Forever | ServicePoint.ConnectionLeaseTimeout | SocketsHttpHandler.PooledConnectionLifetime |
In .NET Framework
Set ServicePoint.ConnectionLeaseTimeout to change the max connection lifetime:
//create the single instance
httpClient = new HttpClient();
var sp = ServicePointManager.FindServicePoint(new Uri("https://localhost:9000"));
sp.ConnectionLeaseTimeout = (int)TimeSpan.FromMinutes(5).TotalMilliseconds;
In .NET Core
Set SocketsHttpHandler.PooledConnectionLifetime to change the max connection lifetime:
var socketHttpHandler = new SocketsHttpHandler()
{
PooledConnectionLifetime = TimeSpan.FromMinutes(5),
};
httpClient = new HttpClient(socketHttpHandler);