55
66using System . ComponentModel ;
77using System . Dynamic ;
8+ using System . Net ;
89using System . Net . Http . Headers ;
910using System . Text ;
1011using System . Web ;
@@ -25,8 +26,8 @@ public class PveClientBase(string host, int port = 8006, HttpClient? httpClient
2526 private ILogger < PveClientBase > _logger = NullLoggerFactory . Instance . CreateLogger < PveClientBase > ( ) ;
2627 private ILoggerFactory _loggerFactory ;
2728
28- private HttpClient _httpClient ;
29- private readonly HttpClient ? _externalHttpClient = httpClient ;
29+ private HttpClient _internalHttpClient ;
30+ private HttpClientHandler _internalHttpClientHandler ;
3031
3132 /// <summary>
3233 /// Logger Factory
@@ -112,6 +113,12 @@ public async Task<bool> LoginAsync(string userName, string password, string real
112113
113114 CSRFPreventionToken = result . Response . data . CSRFPreventionToken ;
114115 PVEAuthCookie = result . Response . data . ticket ;
116+
117+ // Add cookie to CookieContainer for proper authentication in subsequent requests
118+ if ( _internalHttpClientHandler ? . CookieContainer != null )
119+ {
120+ _internalHttpClientHandler . CookieContainer . Add ( new Uri ( BaseAddress ) , new Cookie ( "PVEAuthCookie" , PVEAuthCookie ) ) ;
121+ }
115122 }
116123
117124 return result . IsSuccessStatusCode ;
@@ -181,16 +188,21 @@ public async Task<Result> DeleteAsync(string resource, IDictionary<string, objec
181188 /// <returns></returns>
182189 public virtual HttpClient GetHttpClient ( )
183190 {
184- if ( _externalHttpClient != null ) return _externalHttpClient ;
191+ if ( httpClient != null ) return httpClient ;
185192
186- _httpClient ??= new HttpClient ( new HttpClientHandler
193+ if ( _internalHttpClient == null )
187194 {
188- ServerCertificateCustomValidationCallback = ! ValidateCertificate
189- ? ( message , cert , chain , errors ) => true
190- : null
191- } ) ;
195+ _internalHttpClientHandler = new HttpClientHandler
196+ {
197+ CookieContainer = new CookieContainer ( ) ,
198+ ServerCertificateCustomValidationCallback = ! ValidateCertificate
199+ ? ( message , cert , chain , errors ) => true
200+ : null
201+ } ;
202+ _internalHttpClient = new HttpClient ( _internalHttpClientHandler ) ;
203+ }
192204
193- return _httpClient ;
205+ return _internalHttpClient ;
194206 }
195207
196208 /// <summary>
@@ -250,7 +262,7 @@ protected virtual async Task<Result> ExecuteRequestAsync(string resource,
250262 _logger . LogDebug ( "Method: {httpMethod}, Url: {uriString}" , httpMethod , uriString ) ;
251263 if ( httpMethod != HttpMethod . Get )
252264 {
253- var sensitiveParams = new [ ] { "password" , "token" , "ticket" , "otp" , "apitoken" } ;
265+ var sensitiveParams = new [ ] { "password" , "token" , "ticket" , "otp" , "apitoken" } ;
254266 _logger . LogDebug ( "Parameters: {parameters}" , string . Join ( Environment . NewLine , @params . Select ( a =>
255267 {
256268 var paramName = a . Key . ToLower ( ) ;
@@ -302,6 +314,20 @@ protected virtual async Task<Result> ExecuteRequestAsync(string resource,
302314 catch ( TaskCanceledException ex ) when ( ! cts . Token . IsCancellationRequested )
303315 {
304316 _logger . LogError ( ex , ex . Message ) ;
317+
318+ response = new ( HttpStatusCode . RequestTimeout )
319+ {
320+ ReasonPhrase = ex . Message ,
321+ } ;
322+ }
323+ catch ( Exception ex )
324+ {
325+ _logger . LogError ( ex , ex . Message ) ;
326+
327+ response = new ( HttpStatusCode . InternalServerError )
328+ {
329+ ReasonPhrase = ex . Message ,
330+ } ;
305331 }
306332
307333 if ( _logger . IsEnabled ( LogLevel . Debug ) )
@@ -378,14 +404,14 @@ public async Task<bool> WaitForTaskToFinishAsync(string task, int wait = 500, lo
378404 if ( timeout < wait ) { timeout = wait + 5000 ; }
379405 var timeStart = DateTime . Now ;
380406
381- while ( isRunning && ( DateTime . Now - timeStart ) . Milliseconds < timeout )
407+ while ( isRunning && ( DateTime . Now - timeStart ) . TotalMilliseconds < timeout )
382408 {
383- Thread . Sleep ( wait ) ;
409+ await Task . Delay ( wait ) ;
384410 isRunning = await TaskIsRunningAsync ( task ) ;
385411 }
386412
387413 //check timeout
388- return ( DateTime . Now - timeStart ) . Milliseconds < timeout ;
414+ return ( DateTime . Now - timeStart ) . TotalMilliseconds < timeout ;
389415 }
390416
391417 /// <summary>
0 commit comments