DotNetCampus.HttpClientOverOpenSsl 1.0.0-alpha.2

This is a prerelease version of DotNetCampus.HttpClientOverOpenSsl.
dotnet add package DotNetCampus.HttpClientOverOpenSsl --version 1.0.0-alpha.2
                    
NuGet\Install-Package DotNetCampus.HttpClientOverOpenSsl -Version 1.0.0-alpha.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DotNetCampus.HttpClientOverOpenSsl" Version="1.0.0-alpha.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotNetCampus.HttpClientOverOpenSsl" Version="1.0.0-alpha.2" />
                    
Directory.Packages.props
<PackageReference Include="DotNetCampus.HttpClientOverOpenSsl" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DotNetCampus.HttpClientOverOpenSsl --version 1.0.0-alpha.2
                    
#r "nuget: DotNetCampus.HttpClientOverOpenSsl, 1.0.0-alpha.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DotNetCampus.HttpClientOverOpenSsl@1.0.0-alpha.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DotNetCampus.HttpClientOverOpenSsl&version=1.0.0-alpha.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=DotNetCampus.HttpClientOverOpenSsl&version=1.0.0-alpha.2&prerelease
                    
Install as a Cake Tool

DotNetCampus.HttpClientOverOpenSsl

English

让 HttpClient 的 HTTPS 请求通过 OpenSSL 原生 DLL(libssl-3)完成 TLS 握手和数据传输。

简介

DotNetCampus.HttpClientOverOpenSsl 提供了一个基于 SocketsHttpHandler + OpenSSL 的 HttpMessageHandler,将 HTTPS 请求的 TLS 层交给 OpenSSL 原生库处理,而非依赖 .NET 内置的 SslStream。

核心类是 OpenSslSocketsHttpHandler(继承自 HttpMessageHandler)和便捷封装 OpenSslHttpClient(继承自 HttpClient)。

使用方式

1. 安装 NuGet 包

dotnet add package DotNetCampus.HttpClientOverOpenSsl

2. 准备 OpenSSL 原生 DLL

本库依赖 OpenSSL 3.x 的原生库文件。你可以选择以下两种方式之一:

方式一:安装 openssl-native NuGet 包(推荐)
dotnet add package openssl-native

此包会自动根据运行时平台(x86 / x64 / arm64)将对应的 DLL 复制到输出目录。

方式二:手动部署 DLL

从 OpenSSL 官方 或 slproweb 下载预编译的 Windows 版本,或自编译获取以下文件,放置到应用程序的输出目录:

平台 libssl 文件 libcrypto 文件
Windows x64 libssl-3-x64.dll libcrypto-3-x64.dll
Windows x86 libssl-3.dll libcrypto-3.dll
Windows arm64 libssl-3-arm64.dll libcrypto-3-arm64.dll

库在加载时会按 AppContext.BaseDirectory → runtimes/{rid}/native/ → 自定义 FallbackLibraryPath 的顺序查找上述 DLL。如需指定自定义路径,可设置 OpenSSLNative.FallbackLibraryPath。

3. 在 HttpClient 中使用

方式一:使用 OpenSslHttpClient(推荐)

OpenSslHttpClient 继承自 HttpClient,内部已预配置 OpenSslSocketsHttpHandler,开箱即用:

using var client = new OpenSslHttpClient();
var html = await client.GetStringAsync("https://www.baidu.com");

用法与标准 HttpClient 完全一致,所有 HttpClient 的方法均可直接调用:

using var client = new OpenSslHttpClient();

// GET 请求
var response = await client.GetAsync("https://example.com/api/data");

// POST 请求
var content = new StringContent("{\"key\":\"value\"}", Encoding.UTF8, "application/json");
response = await client.PostAsync("https://example.com/api/submit", content);

// 自定义请求
using var request = new HttpRequestMessage(HttpMethod.Get, "https://example.com");
request.Headers.Add("X-Custom-Header", "value");
response = await client.SendAsync(request);
方式二:使用 OpenSslSocketsHttpHandler

如果需要更精细的控制(例如自定义 SocketsHttpHandler 的超时、代理、Cookie 等设置),可以直接使用 OpenSslSocketsHttpHandler:

var innerHandler = new SocketsHttpHandler
{
    UseCookies = false,
    MaxConnectionsPerServer = 32,
    PooledConnectionLifetime = TimeSpan.FromMinutes(2)
};

using var handler = new OpenSslSocketsHttpHandler(innerHandler);
using var client = new HttpClient(handler);
var response = await client.GetAsync("https://example.com");

OpenSslSocketsHttpHandler 仅处理 HTTPS 请求的 TLS 层。HTTP(明文)请求不受影响,走默认 TCP 连接。

许可证

MIT

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

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
1.0.0-alpha.2 98 7/9/2026
1.0.0-alpha.1 74 7/8/2026