TlsClient.Native runs the tls-client engine inside your .NET process. Use it when you can deploy the native library alongside your app. The library must match the OS and architecture of the process running your app.
Install the .NET wrapper and the native runtime package. This example uses Windows x64.
dotnet add package TlsClient.Native
dotnet add package TlsClient.Native.win-x64The runtime package copies the library when you build with the matching runtime identifier. On Windows x64, run the example with dotnet run --runtime win-x64.
using System;
using System.IO;
using TlsClient.Core.Builders;
using TlsClient.Core.Models.Entities;
using TlsClient.Core.Models.Requests;
using TlsClient.Native;
using TlsClient.Native.Extensions;
var libraryPath = args.Length > 0
? Path.GetFullPath(args[0])
: Path.Combine(AppContext.BaseDirectory, "runtimes", "tls-client", "win", "x64", "tls-client.dll");
NativeTlsClient.Initialize(libraryPath);
using var client = new TlsClientBuilder()
.WithIdentifier(TlsClientIdentifier.Chrome133)
.WithTimeout(TimeSpan.FromSeconds(15))
.Build();
var response = client.Request(new Request
{
RequestUrl = "https://example.com/"
});
Console.WriteLine($"{(int)response.Status} {response.Status}");
Console.WriteLine(response.Body);Initialize the library once at process startup. The builder can also initialize it with .WithNative(libraryPath) before .Build().
Choose the package and runtime identifier for your target platform. The table uses the IDs produced by the current native package build.
| Platform | .NET runtime identifier | Native package |
|---|---|---|
| Windows x64 | win-x64 |
TlsClient.Native.win-x64 |
| Windows x86 | win-x86 |
TlsClient.Native.win-x86 |
| Linux x64 | linux-x64 |
TlsClient.Native.linux-amd64 |
| Linux x86 | linux-x86 |
TlsClient.Native.linux-386 |
| Linux Arm64 | linux-arm64 |
TlsClient.Native.linux-arm64 |
| Linux Armv7 | linux-arm |
TlsClient.Native.linux-armv7 |
| macOS Intel | osx-x64 |
TlsClient.Native.darwin-amd64 |
| macOS Apple silicon | osx-arm64 |
TlsClient.Native.darwin-arm64 |
You can also download a shared library from the upstream releases. Pass the full path to the example with dotnet run -- /path/to/tls-client.so, using the .dll, .so, or .dylib for your platform.
Run each example after initialization. The snippets that use client reuse the client from Quick start. Add using System.Net.Http; for the JSON example.
The first request initializes the session and stores a server cookie. Use AddCookies to seed the same session with a cookie and GetCookies to inspect the cookies for a URL. Set Expires = null for a session cookie.
const string cookiesUrl = "https://httpbin.io/cookies";
client.Request(new Request { RequestUrl = cookiesUrl + "/set?theme=dark" });
client.AddCookies(cookiesUrl, new()
{
new TlsClientCookie("locale", "en", "httpbin.io") { Path = "/", Expires = null }
});
var cookieResponse = client.Request(new Request { RequestUrl = cookiesUrl });
Console.WriteLine(cookieResponse.Body);
foreach (var cookie in client.GetCookies(cookiesUrl).Cookies ?? new())
Console.WriteLine(cookie.Name);Reuse the same client to send stored cookies on later requests. Dispose it when you finish with the session. Cookie tests
RequestBuilder.WithBody(object) serializes the object as JSON. Set the content type on the request.
var jsonRequest = new RequestBuilder()
.WithUrl("https://httpbin.io/post")
.WithMethod(HttpMethod.Post)
.WithHeader("Content-Type", "application/json")
.WithBody(new { title = "Hello from .NET", published = true })
.Build();
var jsonResponse = client.Request(jsonRequest);
Console.WriteLine(jsonResponse.Body);Build a client for the proxy as shown below. The CONNECT header order applies to the handshake with the proxy for an HTTPS destination.
using var proxyClient = new TlsClientBuilder()
.WithIdentifier(TlsClientIdentifier.Chrome133)
.WithProxyUrl("http://127.0.0.1:8086")
.WithConnectHeader("X-Client", "my-app")
.WithConnectHeader("X-Request-Id", "demo")
.WithConnectHeaderOrder(new() { "X-Request-Id", "X-Client" })
.Build();
var proxyResponse = proxyClient.Request(new Request { RequestUrl = "https://example.com/" });
Console.WriteLine($"{(int)proxyResponse.Status} {proxyResponse.Status}");Replace the proxy address with your own. CONNECT ordering test
WithTimeout sets the overall request deadline. ResponseHeaderTimeout limits how long the client waits for response headers over HTTP/1.1 and HTTP/2. Set transport options before opening the session.
using var timedClient = new TlsClientBuilder()
.WithIdentifier(TlsClientIdentifier.Chrome133)
.WithTimeout(TimeSpan.FromSeconds(15))
.WithTransportOptions(new TransportOptions
{
ResponseHeaderTimeout = TimeSpan.FromSeconds(1)
})
.Build();
var timedResponse = timedClient.Request(new Request { RequestUrl = "https://httpbin.io/delay/3" });
Console.WriteLine($"{(int)timedResponse.Status} {timedResponse.Body}");RequestAsync runs the native call on a worker thread. Its cancellation token can prevent a call from starting. Use the request timeout to bound the native call. Timeout test
Use a writable path. This example creates a unique destination because the engine appends streamed bytes to the file.
var imagePath = Path.Combine(Path.GetTempPath(), $"tlsclient-{Guid.NewGuid():N}.png");
var download = client.Request(new Request
{
RequestUrl = "https://httpbin.io/image/png",
StreamOutputPath = imagePath
});
if ((int)download.Status == 200)
Console.WriteLine($"Saved to {imagePath}");
else
Console.WriteLine(download.Body);If Status is 0, check Body for the failure message. For a library load error, check the file path, process architecture, and native dependencies.
See the project overview and native integration tests for more examples.