1- using Http2Client . Builders ;
1+ using Http2Client . Builders ;
22using Http2Client . Core . Enums ;
33using Http2Client . Core . Request ;
44
55using System ;
6+ using System . Collections . Generic ;
7+ using System . Text . Json ;
68
79internal 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