A native (in-process) client for TlsClient.NET.
dotnet add package TlsClient.NativeInitialize the native library once at process start:
using TlsClient.Native;
NativeTlsClient.Initialize("{PATH_TO_NATIVE_LIBRARY}");
// e.g. Windows: "C:\\tools\\tls-client\\tls-client-windows-64-1.11.0.dll"using TlsClient.Native;
using TlsClient.Core.Models.Requests;
NativeTlsClient.Initialize("{PATH_TO_NATIVE_LIBRARY}");
using var client = new NativeTlsClient(); // or pass TlsClientOptions
var res = client.Request(new Request {
RequestUrl = "https://httpbin.io/get"
});
Console.WriteLine($"{(int)res.Status} {res.Status}");
Console.WriteLine(res.Body);For advanced configuration, construct with
new NativeTlsClient(new TlsClientOptions(...)).
public sealed class NativeTlsClient : BaseTlsClient
{
// Static bootstrap
public static void Initialize(string? libraryPath);
// Constructors
public NativeTlsClient(TlsClientOptions options);
public NativeTlsClient();
// Sync
public override Response Request(Request request);
public override GetCookiesFromSessionResponse GetCookies(string url);
public override GetCookiesFromSessionResponse AddCookies(string url, List<TlsClientCookie> cookies);
public override DestroyResponse Destroy();
public override DestroyResponse DestroyAll();
// Async (wrapping sync)
public override Task<Response> RequestAsync(Request request, CancellationToken ct = default);
public override Task<GetCookiesFromSessionResponse> GetCookiesAsync(string url, CancellationToken ct = default);
public override Task<GetCookiesFromSessionResponse> AddCookiesAsync(string url, List<TlsClientCookie> cookies, CancellationToken ct = default);
public override Task<DestroyResponse> DestroyAsync(CancellationToken ct = default);
public override Task<DestroyResponse> DestroyAllAsync(CancellationToken ct = default);
}Use the shared TlsClientBuilder to configure common options, then switch to the native transport with WithNative(...) and call Build() to get a NativeTlsClient.
// 1) Initialize native library once
NativeTlsClient.Initialize("C:\\tools\\tls-client\\tls-client-windows-64-1.11.0.dll");
// 2) Create client
using var client = new TlsClientBuilder()
.WithIdentifier(TlsClientIdentifier.Chrome133)
.WithUserAgent("MyApp/1.0")
.WithFollowRedirects()
.WithTimeout(TimeSpan.FromSeconds(15))
.WithDefaultCookieJar()
.WithHeader("Accept-Language", "en-US,en;q=0.9")
.WithProxyUrl("http://127.0.0.1:8086", isRotating: false);
.withNative() // OPTIONAL
.build()
// 3) Make a request
var response = await client.RequestAsync(new Request
{
RequestUrl = "https://httpbin.io/get"
});
⚠️ Important:
- Call
NativeTlsClient.Initialize(path)once per project before creating clients.- You can call
.WithNative(path)if you dont want useNativeTlsClient.Initialize(path).- Due to C# ↔ Go interop, native mode can be less stable under heavy concurrency. If you run into issues, prefer the API mode.
-
Initialize(path)loads the native library viaTlsClientWrapper.Initialize(...). -
Request(request):- Merges client defaults via
PrepareRequest(...). - Serializes with
RequestHelpers.Prepare(...), callsTlsClientWrapper.Request(...). - Deserializes to
Responseand frees native memory withTlsClientWrapper.FreeMemory(response.Id)when present. - Normalizes timeouts: if
Status == 0andBodycontains"Client.Timeout exceeded", returns408 RequestTimeout.
- Merges client defaults via
-
Cookie and destroy operations map to corresponding wrapper functions and return strongly-typed results.
- Request highlights:
RequestUrl(required),RequestMethod,Headers,RequestCookies,RequestBody(string or bytes viaRequestHelpers.PrepareBody(...)),IsByteRequest/IsByteResponse,StreamOutputPath, TLS/transport flags (TlsClientIdentifier,WithDefaultCookieJar,WithoutCookieJar,FollowRedirects,Timeout*, etc.). - Response:
Status(HttpStatusCode),Body(string?, empty if streamed to file),Headers(Dictionary<string, List<string>>),Id(internal handle used to free native buffers).
- Native failures set
Response.Status = 0and include the error message inResponse.Body. "Client.Timeout exceeded"is normalized to408 RequestTimeout.- Always call
Initialize(...)before the first request. - Ensure
StreamOutputPathtargets a writable location.
This page focuses on the API surface and basic usage.
For additional scenarios, please refer to the test projects (e.g., BodyTests, CookieTests).
MIT — see the root LICENSE file.