Skip to content

Commit 757e56f

Browse files
committed
refactor!: update API and extend functionality
1 parent 3f99a9c commit 757e56f

28 files changed

+1029
-766
lines changed

README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Http2Client
77

8-
Http2Client is a fork of [TlsClient.NET](https://github.com/ErenKrt/TlsClient.NET), providing customizable HTTP/2 clients with TLS fingerprinting capabilities. Based on [bogdanfinn/tls-client](https://github.com/bogdanfinn/tls-client/), it allows you to mimic specific browser fingerprints and control detailed aspects of TLS behavior in your .NET applications.
8+
Http2Client provides customizable HTTP/2 clients with TLS fingerprinting capabilities. Based on [bogdanfinn/tls-client](https://github.com/bogdanfinn/tls-client/), it allows you to mimic specific browser fingerprints and control detailed aspects of TLS behavior in your .NET applications.
99

1010
## Installation
1111

@@ -92,7 +92,7 @@ var cookies = client.GetCookies("https://example.com");
9292
// Add cookies to session
9393
var newCookies = new List<ClientCookie>
9494
{
95-
new ClientCookie { Name = "session", Value = "abc123" }
95+
new ClientCookie("session", "abc123")
9696
};
9797
client.AddCookies("https://example.com", newCookies);
9898

@@ -125,30 +125,35 @@ using var client = new HttpClientBuilder()
125125
| `WithProxy(string, bool)` | Configures proxy URL and rotation setting. |
126126
| `WithInsecureSkipVerify(bool)` | Skips SSL certificate verification. |
127127
| `WithRandomTlsExtensions(bool)` | Randomizes TLS extension order. |
128-
| `WithCookies(bool)` | Enables or disables automatic cookie handling. |
128+
| `WithCookies(bool)` | Enables automatic cookie handling. |
129+
| `WithoutCookieJar(bool)` | Disables all cookie handling. |
129130
| `WithDebug(bool)` | Enables debug logging. |
130-
| `DisableIPv4(bool)` | Disables IPv4 connections. |
131-
| `DisableIPv6(bool)` | Disables IPv6 connections. |
132-
| `FollowRedirects(bool)` | Enables automatic redirect following. |
133-
| `ForceHttp1(bool)` | Forces HTTP/1.1 instead of HTTP/2. |
134-
| `SetHeader(string, string)` | Sets a default header. |
131+
| `WithDisableIPv4(bool)` | Disables IPv4 connections. |
132+
| `WithDisableIPv6(bool)` | Disables IPv6 connections. |
133+
| `WithFollowRedirects(bool)` | Enables automatic redirect following. |
134+
| `WithForceHttp1(bool)` | Forces HTTP/1.1 instead of HTTP/2. |
135+
| `WithHeader(string, string)` | Sets a default header. |
135136
| `WithHeaders(Dictionary<string, string>)` | Sets multiple default headers. |
137+
| `WithHeaderOrder(params string[])` | Sets the order of HTTP headers. |
138+
| `WithSessionId(Guid)` | Sets the session ID for this client. |
139+
| `WithCustomHttp2Client(CustomHttp2Client)` | Uses custom TLS fingerprint. |
140+
| `WithCatchPanics(bool)` | Catches native library panics. |
136141

137142
### Advanced Example
138143

139144
```csharp
140145
using var client = new HttpClientBuilder()
141146
.WithLibraryPath("tls-client-windows-64-1.11.0.dll")
142-
.WithBrowserType(BrowserType.Firefox133)
147+
.WithBrowserType(BrowserType.Firefox132)
143148
.WithUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64)")
144149
.WithProxy("http://127.0.0.1:8888", isRotating: true)
145150
.WithTimeout(TimeSpan.FromSeconds(30))
146151
.WithDebug(true)
147-
.FollowRedirects(true)
152+
.WithFollowRedirects(true)
148153
.WithInsecureSkipVerify(false)
149-
.DisableIPv6(true)
150-
.SetHeader("X-Custom-Header", "MyValue")
151-
.WithCookies(true)
154+
.WithDisableIPv6(true)
155+
.WithHeader("X-Custom-Header", "MyValue")
156+
.WithCookies()
152157
.Build();
153158
```
154159

@@ -163,7 +168,7 @@ For more complex request configuration, use the fluent `HttpRequestBuilder`:
163168
```csharp
164169
var request = new HttpRequestBuilder()
165170
.WithUrl("https://api.example.com/data")
166-
.WithMethod("POST")
171+
.WithMethod(HttpMethod.Post)
167172
.WithJsonBody(new { name = "John", age = 30 })
168173
.WithBrowserType(BrowserType.Chrome133)
169174
.WithTimeout(TimeSpan.FromSeconds(30))
@@ -201,9 +206,12 @@ var request = new HttpRequest
201206
| `Headers` | Dictionary of HTTP headers. |
202207
| `BrowserType` | Browser fingerprint to use. |
203208
| `TimeoutMilliseconds` | Request timeout in milliseconds. |
209+
| `TimeoutSeconds` | Request timeout in seconds. |
204210
| `ProxyUrl` | Proxy server URL. |
205211
| `InsecureSkipVerify` | Skip SSL certificate verification. |
206212
| `FollowRedirects` | Follow HTTP redirects automatically. |
213+
| `RequestCookies` | List of cookies for this request. |
214+
| `WithDebug` | Enable debug logging for this request. |
207215

208216
## Features
209217

examples/Program.cs

Lines changed: 170 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,196 @@
1-
using Http2Client.Builders;
1+
using Http2Client.Builders;
22
using Http2Client.Core.Enums;
33
using Http2Client.Core.Request;
44

55
using System;
6+
using System.Collections.Generic;
7+
using System.Text.Json;
68

79
internal class Program
810
{
911
private const string PATH_LIB = "Native\\tls-client-windows-64-1.11.0.dll";
1012

11-
private static void Main(string[] args)
13+
private static void Main()
1214
{
13-
using var client = CreateClientBuilder().Build();
15+
//BasicGetRequest();
16+
//PostJsonRequest();
17+
//CookieHandling();
18+
//HeadersAndProxy();
19+
ErrorHandlingAndTimeouts();
1420

15-
var request = new HttpRequest()
21+
Console.ReadLine();
22+
}
23+
24+
private static void BasicGetRequest()
25+
{
26+
Console.WriteLine("Basic GET request:");
27+
28+
using var client = new HttpClientBuilder()
29+
.WithUserAgent("Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36")
30+
.WithLibraryPath(PATH_LIB)
31+
.WithRandomTlsExtensions()
32+
.Build();
33+
34+
var request = new HttpRequest
1635
{
1736
RequestUrl = "https://tls.peet.ws/api/all",
1837
RequestMethod = "GET",
38+
BrowserType = BrowserType.Chrome133
39+
};
40+
41+
var response = client.Send(request);
42+
Console.WriteLine($"Status: {response?.Status}");
43+
Console.WriteLine($"Response size: {response?.Body?.Length ?? 0} chars");
44+
Console.WriteLine();
45+
}
46+
47+
private static void PostJsonRequest()
48+
{
49+
Console.WriteLine("POST request with JSON:");
50+
51+
using var client = new HttpClientBuilder()
52+
.WithUserAgent("Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36")
53+
.WithLibraryPath(PATH_LIB)
54+
.WithHeader("Content-Type", "application/json")
55+
.WithCookies()
56+
.Build();
57+
58+
var jsonData = new { name = "Test User", email = "test@example.com" };
59+
var jsonString = JsonSerializer.Serialize(jsonData);
60+
61+
var request = new HttpRequest
62+
{
63+
RequestUrl = "https://httpbin.org/post",
64+
RequestMethod = "POST",
65+
RequestBody = jsonString,
1966
BrowserType = BrowserType.Chrome133,
67+
Headers = new Dictionary<string, string>
68+
{
69+
["Accept"] = "application/json",
70+
["Content-Type"] = "application/json"
71+
}
2072
};
2173

2274
var response = client.Send(request);
75+
Console.WriteLine($"Status: {response?.Status}");
76+
Console.WriteLine($"Sent data: {jsonString}");
77+
Console.WriteLine();
78+
}
79+
80+
private static void CookieHandling()
81+
{
82+
Console.WriteLine("Cookie handling:");
83+
84+
using var client = new HttpClientBuilder()
85+
.WithUserAgent("Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36")
86+
.WithLibraryPath(PATH_LIB)
87+
.WithCookies(true)
88+
.Build();
89+
90+
var request1 = new HttpRequest
91+
{
92+
RequestUrl = "https://httpbin.org/cookies/set/session_id/abc123",
93+
RequestMethod = "GET",
94+
BrowserType = BrowserType.Chrome133
95+
};
96+
97+
var response1 = client.Send(request1);
98+
Console.WriteLine($"First request (set cookies): {response1?.Status}");
2399

24-
Console.WriteLine(response.Status);
25-
Console.WriteLine(response.Body);
100+
var cookies = client.GetCookies("https://httpbin.org");
101+
Console.WriteLine($"Got cookies: {cookies?.Cookies?.Count ?? 0}");
102+
103+
var request2 = new HttpRequest
104+
{
105+
RequestUrl = "https://httpbin.org/cookies",
106+
RequestMethod = "GET",
107+
BrowserType = BrowserType.Chrome133
108+
};
109+
110+
var response2 = client.Send(request2);
111+
Console.WriteLine($"Second request (with cookies): {response2?.Status}");
112+
Console.WriteLine();
26113
}
27114

28-
private static HttpClientBuilder CreateClientBuilder()
115+
private static void HeadersAndProxy()
29116
{
30-
//const string Proxy = "https://localhost:1777";
31-
const string UserAgent = "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36";
117+
Console.WriteLine("Custom headers:");
32118

33-
return new HttpClientBuilder()
34-
.WithUserAgent(UserAgent)
35-
.WithCookies()
119+
using var client = new HttpClientBuilder()
120+
.WithUserAgent("Custom-Agent/1.0")
36121
.WithLibraryPath(PATH_LIB)
37-
.WithRandomTlsExtensions();
122+
.WithHeader("Accept", "application/json")
123+
.WithHeader("Accept-Language", "en-US,en;q=0.9")
124+
.WithHeader("Accept-Encoding", "gzip, deflate, br")
125+
.WithHeaderOrder("User-Agent", "Accept", "Accept-Language", "Accept-Encoding")
126+
.WithFollowRedirects(true)
127+
.Build();
128+
129+
var request = new HttpRequest
130+
{
131+
RequestUrl = "https://httpbin.org/headers",
132+
RequestMethod = "GET",
133+
BrowserType = BrowserType.Chrome133,
134+
Headers = new Dictionary<string, string>
135+
{
136+
["X-Custom-Header"] = "CustomValue",
137+
["X-Request-ID"] = Guid.NewGuid().ToString()
138+
}
139+
};
140+
141+
var response = client.Send(request);
142+
Console.WriteLine($"Status: {response?.Status}");
143+
Console.WriteLine("Custom headers sent");
144+
Console.WriteLine();
145+
}
146+
147+
private static void ErrorHandlingAndTimeouts()
148+
{
149+
Console.WriteLine("Error handling and timeouts:");
150+
151+
using var client = new HttpClientBuilder()
152+
.WithUserAgent("Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36")
153+
.WithLibraryPath(PATH_LIB)
154+
.WithTimeout(TimeSpan.FromSeconds(10))
155+
.WithCatchPanics(true)
156+
.WithInsecureSkipVerify(false)
157+
.WithDebug(false)
158+
.Build();
159+
160+
try
161+
{
162+
var request = new HttpRequest
163+
{
164+
RequestUrl = "https://httpbin.org/delay/2",
165+
RequestMethod = "GET",
166+
BrowserType = BrowserType.Chrome133
167+
};
168+
169+
var response = client.Send(request);
170+
Console.WriteLine($"Delayed request: {response?.Status}");
171+
}
172+
catch (Exception ex)
173+
{
174+
Console.WriteLine($"Request error: {ex.Message}");
175+
}
176+
177+
try
178+
{
179+
var request = new HttpRequest
180+
{
181+
RequestUrl = "https://nonexistent-domain-12345.com",
182+
RequestMethod = "GET",
183+
BrowserType = BrowserType.Chrome133
184+
};
185+
186+
var response = client.Send(request);
187+
Console.WriteLine($"Nonexistent domain: {response?.Status}");
188+
}
189+
catch (Exception ex)
190+
{
191+
Console.WriteLine($"Expected error: {ex.Message.Split('\n')[0]}");
192+
}
193+
194+
Console.WriteLine();
38195
}
39196
}

0 commit comments

Comments
 (0)