SecureHttpClient 2.2.6
dotnet add package SecureHttpClient --version 2.2.6
NuGet\Install-Package SecureHttpClient -Version 2.2.6
<PackageReference Include="SecureHttpClient" Version="2.2.6" />
paket add SecureHttpClient --version 2.2.6
#r "nuget: SecureHttpClient, 2.2.6"
// Install SecureHttpClient as a Cake Addin #addin nuget:?package=SecureHttpClient&version=2.2.6 // Install SecureHttpClient as a Cake Tool #tool nuget:?package=SecureHttpClient&version=2.2.6
SecureHttpClient
SecureHttpClient is a dotnet cross-platform HttpClientHandler library, with additional security features.
Features
Feature | Android | iOS | Windows |
---|---|---|---|
Certificate pinning | ✅ | ✅ | ✅ |
TLS 1.2+ | ✅ | ✅ | ✅ |
HTTP/2 | ✅ | ✅ | ✅ |
Compression (gzip / deflate / br) | ✅ | ✅ | ✅ |
Client certificates | ✅ | ✅ | ✅ |
Headers ordering | ✅ | ❌ | ❌ |
Cookies | ✅ | ✅ | ✅ |
Installation
The most recent version is available (and is tested) on the following platforms:
- Android 5-14 (API 21-34)
- iOS 17.2
- .net 8.0
Older versions support older frameworks (but they are not maintained anymore):
- v2.1: net7.0 (android / ios / windows)
- v2.0: net6.0 (android / ios / windows)
- v1.x: MonoAndroid ; Xamarin.iOS ; NetStandard
Basic usage
Basic usage is similar to using System.Net.Http.HttpClientHandler
.
// create the SecureHttpClientHandler
var secureHttpClientHandler = new SecureHttpClientHandler(null);
// create the HttpClient
var httpClient = new HttpClient(secureHttpClientHandler);
// example of a simple GET request
var response = await httpClient.GetAsync("https://www.github.com");
var html = await response.Content.ReadAsStringAsync();
Certificate pining
After creating a SecureHttpClientHandler
object, call AddCertificatePinner
to add one or more certificate pinner.
The request will fail if the certificate pin is not correct.
// create the SecureHttpClientHandler
var secureHttpClientHandler = new SecureHttpClientHandler(null);
// add certificate pinner
secureHttpClientHandler.AddCertificatePinner("www.github.com", ["sha256/YH8+l6PDvIo1Q5o6varvw2edPgfyJFY5fHuSlsVdvdc="]);
// create the HttpClient
var httpClient = new HttpClient(secureHttpClientHandler);
// example of a simple GET request
var response = await httpClient.GetAsync("https://www.github.com");
var html = await response.Content.ReadAsStringAsync();
In order to compute the pin (SPKI fingerprint of the server's SSL certificate), you can execute the following command (here for www.github.com
host):
openssl s_client -connect www.github.com:443 -servername www.github.com | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | openssl x509 -noout -pubkey | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64
Cookies and Redirect
SecureHttpClient handles cookies and redirects, but the behavior can differ a bit from one platform to another, because of different implementations in the native libraries used internally.
For strictly identical behavior between platforms, it's recommended to use Flurl on top of SecureHttpClient, and let it handle cookies and redirects.
// create the SecureHttpClientHandler
var secureHttpClientHandler = new SecureHttpClientHandler(null);
// disable redirect and cookies management in this handler
secureHttpClientHandler.AllowAutoRedirect = false;
secureHttpClientHandler.UseCookies = false;
// create the FlurlClient and CookieSession, they will manage redirect and cookies
var httpClient = new HttpClient(secureHttpClientHandler);
var flurlClient = new FlurlClient(httpClient);
var flurlSession = new CookieSession(flurlClient);
// example of a simple GET request using Flurl
var html = await flurlSession
.Request("https://www.github.com")
.GetStringAsync();
Advanced usage
For more advanced usage (logging, client certificates, cookies ordering...), have a look into the SecureHttpClient.Test folder for more code examples.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-android34.0 is compatible. net8.0-browser was computed. net8.0-ios was computed. net8.0-ios17.2 is compatible. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
-
net8.0-android34.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
- Square.OkHttp3 (>= 4.12.0.4)
- Square.OkHttp3.UrlConnection (>= 4.12.0.4)
- Square.OkIO (>= 3.9.0)
-
net8.0-ios17.2
- BouncyCastle.Cryptography (>= 2.4.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.2.6 | 480 | 6/24/2024 |
2.2.5 | 273 | 3/13/2024 |
2.2.4 | 133 | 3/6/2024 |
2.2.3 | 128 | 2/20/2024 |
2.2.2 | 167 | 1/15/2024 |
2.2.1 | 234 | 11/22/2023 |
2.2.0 | 269 | 11/17/2023 |
2.1.3 | 710 | 6/2/2023 |
2.1.2 | 2,452 | 4/25/2023 |
2.1.1 | 244 | 3/3/2023 |
2.1.0 | 304 | 12/19/2022 |
2.0.1 | 377 | 10/27/2022 |
2.0.0 | 360 | 10/25/2022 |
1.18.8 | 1,060 | 10/25/2022 |
1.18.7 | 514 | 7/25/2022 |
1.18.6 | 532 | 3/17/2022 |
1.18.5 | 442 | 11/15/2021 |
1.18.4 | 404 | 10/16/2021 |
1.18.3 | 450 | 6/1/2021 |
1.18.2 | 384 | 5/26/2021 |
1.18.1 | 488 | 2/25/2021 |
1.18.0 | 389 | 2/19/2021 |
1.17.5 | 422 | 2/10/2021 |
1.17.4 | 413 | 2/1/2021 |
1.17.2 | 410 | 1/26/2021 |
1.17.1 | 431 | 1/18/2021 |
1.17.0 | 420 | 1/13/2021 |
1.16.1 | 1,591 | 12/11/2020 |
1.16.0 | 479 | 12/2/2020 |
1.15.0 | 476 | 11/13/2020 |
1.14.1 | 504 | 10/14/2020 |
1.14.0 | 677 | 4/30/2020 |
1.13.7 | 645 | 4/15/2020 |
1.13.6 | 761 | 3/2/2020 |
1.13.5 | 531 | 2/14/2020 |
1.13.4 | 534 | 2/14/2020 |
1.13.3 | 565 | 1/13/2020 |
1.13.2 | 578 | 12/9/2019 |
1.13.1 | 582 | 10/25/2019 |
1.13.0 | 565 | 10/23/2019 |
1.12.4 | 716 | 6/30/2019 |
1.12.1 | 620 | 6/19/2019 |
1.12.0 | 692 | 3/26/2019 |
1.11.0 | 1,709 | 10/10/2018 |
1.10.0 | 936 | 6/20/2018 |
1.9.0 | 962 | 6/8/2018 |
1.8.0 | 1,010 | 5/25/2018 |
1.7.0 | 1,059 | 4/10/2018 |
1.6.0 | 1,015 | 10/19/2017 |
1.5.1 | 3,524 | 9/27/2017 |
1.5.0 | 1,053 | 7/2/2017 |
1.4.2 | 1,099 | 11/30/2016 |
1.4.1 | 1,058 | 11/25/2016 |
1.3.0 | 1,055 | 10/17/2016 |