Last active
January 7, 2026 21:40
-
-
Save miguelerm/62898372ad0cca2527fb3755dae05700 to your computer and use it in GitHub Desktop.
create a client with Tls12 support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace MyApp.ApiClient; | |
| public class HttpClientApiService(HttpClient httpClient) | |
| { | |
| public async Task<ItemDto?> GetItemAsync(int itemId, CancellationToken cancellationToken = default) | |
| { | |
| // get item from /api/items/{itemId} sending accept: application/json | |
| var request = new HttpRequestMessage(HttpMethod.Get, $"/api/items/{itemId}"); | |
| request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); | |
| var response = await httpClient.SendAsync(request, cancellationToken); | |
| response.EnsureSuccessStatusCode(); | |
| var content = await response.Content.ReadAsStringAsync(cancellationToken); | |
| var item = System.Text.Json.JsonSerializer.Deserialize<ItemDto>(content, new System.Text.Json.JsonSerializerOptions | |
| { | |
| PropertyNameCaseInsensitive = true | |
| }); | |
| return item; | |
| } | |
| } | |
| public record ItemDto(int Id, string Name); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| services.AddSingleton(sp => | |
| { | |
| // Assuming you have configs as a service | |
| var config = sp.GetRequiredService<IAppConfiguration>(); | |
| var handler = new SocketsHttpHandler | |
| { | |
| AllowAutoRedirect = true, | |
| AutomaticDecompression = System.Net.DecompressionMethods.All, | |
| SslOptions = { EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls13 }, | |
| ConnectTimeout = config.HttpConnectTimeout | |
| }; | |
| var client = new HttpClient(handler); | |
| client.DefaultRequestHeaders.Accept.ParseAdd("image/*;q=0.8,*/*;q=0.5"); | |
| client.DefaultRequestHeaders.AcceptLanguage.ParseAdd("en-US,en;q=0.9"); | |
| client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0.0.0 Safari/537.36"); | |
| client.Timeout = config.HttpTotalTimeout; | |
| return client; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var handler = new SocketsHttpHandler | |
| { | |
| AllowAutoRedirect = true, | |
| AutomaticDecompression = System.Net.DecompressionMethods.All, | |
| SslOptions = { EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls13 }, | |
| ConnectTimeout = TimeSpan.FromSeconds(15) | |
| }; | |
| var client = new HttpClient(handler); | |
| client.DefaultRequestHeaders.Accept.ParseAdd("image/*;q=0.8,*/*;q=0.5"); | |
| client.DefaultRequestHeaders.AcceptLanguage.ParseAdd("en-US,en;q=0.9"); | |
| client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0.0.0 Safari/537.36"); | |
| client.Timeout = TimeSpan.FromSeconds(60); | |
| return client; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment