55using System . IO ;
66using System . Net ;
77using System . Net . Http ;
8+ using System . Net . Http . Headers ;
89using System . Reflection ;
910using System . Threading . Tasks ;
1011using System . Windows ;
@@ -33,6 +34,7 @@ public class PrinterModel
3334 public PrinterViewModel PrinterViewModel { get ; } = new ( ) ;
3435
3536 private readonly ConfigFile _configFile ;
37+ private readonly HttpClient _httpClient ;
3638
3739 public PrinterModel ( ConfigFile configFile )
3840 {
@@ -42,6 +44,10 @@ public PrinterModel(ConfigFile configFile)
4244 SecurityProtocolType . Tls |
4345 SecurityProtocolType . Tls11 | SecurityProtocolType . Tls12 ;
4446
47+ _httpClient = new HttpClient ( ) ;
48+ _httpClient . DefaultRequestHeaders . Authorization
49+ = new AuthenticationHeaderValue ( "token" , _configFile . AuthorizationToken ) ;
50+
4551 if ( ! Directory . Exists ( _configFile . TempSavePath ) )
4652 Directory . CreateDirectory ( _configFile . TempSavePath ) ;
4753
@@ -98,9 +104,9 @@ public async void PrintAsync(bool printDialog)
98104 saveFilePath =
99105 saveFilePath . Replace ( Path . DirectorySeparatorChar . ToString ( ) , "/" ) ;
100106 ShowComplement ( ) ;
101- using var client = new HttpClient ( ) ;
102107 await using var s =
103- await client . GetStreamAsync ( "https://cdn.profcomff.com/app/printer/iddqd.pdf" ) ;
108+ await _httpClient . GetStreamAsync (
109+ "https://cdn.profcomff.com/app/printer/iddqd.pdf" ) ;
104110 await using var fs = new FileStream ( saveFilePath , FileMode . OpenOrCreate ) ;
105111 await s . CopyToAsync ( fs ) ;
106112 PrintFile ( saveFilePath , new PrintOptions ( "" , 1 , false ) ,
@@ -117,11 +123,10 @@ public async void PrintAsync(bool printDialog)
117123 $ "{ GetType ( ) . Name } { MethodBase . GetCurrentMethod ( ) ? . Name } : Start response code { PrinterViewModel . CodeTextBoxText } ") ;
118124 var patchFrom = $ "{ FileUrl } /{ PrinterViewModel . CodeTextBoxText } ";
119125 PrinterViewModel . DownloadNotInProgress = false ;
120- var httpClient = new HttpClient ( ) ;
121126 try
122127 {
123128 var response =
124- await httpClient . GetAsync ( $ "{ FileUrl } /{ PrinterViewModel . CodeTextBoxText } ") ;
129+ await _httpClient . GetAsync ( $ "{ FileUrl } /{ PrinterViewModel . CodeTextBoxText } ") ;
125130 if ( response . StatusCode == HttpStatusCode . OK )
126131 {
127132 Marketing . CheckCode (
@@ -197,9 +202,8 @@ private async Task Download(ReceiveOutput fileWithOptions, string patchFrom,
197202 var saveFilePath = _configFile . TempSavePath + Path . DirectorySeparatorChar + name ;
198203 saveFilePath = saveFilePath . Replace ( Path . DirectorySeparatorChar . ToString ( ) , "/" ) ;
199204 ShowComplement ( ) ;
200- using var client = new HttpClient ( ) ;
201205 await using var s =
202- await client . GetStreamAsync ( $ "{ StaticUrl } /{ fileWithOptions . Filename } ") ;
206+ await _httpClient . GetStreamAsync ( $ "{ StaticUrl } /{ fileWithOptions . Filename } ") ;
203207 await using var fs = new FileStream ( saveFilePath , FileMode . OpenOrCreate ) ;
204208 await s . CopyToAsync ( fs ) ;
205209 Marketing . FinishDownload ( pathFrom : patchFrom ,
0 commit comments