-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientManager.cs
More file actions
30 lines (25 loc) · 900 Bytes
/
Copy pathHttpClientManager.cs
File metadata and controls
30 lines (25 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace JustLauncher;
/// <summary>
/// Provides a singleton HttpClient instance to avoid socket exhaustion.
/// Creating multiple HttpClient instances can lead to port exhaustion and DNS issues.
/// </summary>
public static class HttpClientManager
{
private static readonly Lazy<HttpClient> _instance = new(() =>
{
var client = new HttpClient
{
Timeout = TimeSpan.FromSeconds(30)
};
// Set default headers
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
return client;
});
/// <summary>
/// Gets the singleton HttpClient instance.
/// </summary>
public static HttpClient Instance => _instance.Value;
}