@@ -176,55 +176,53 @@ private HttpResponseMessage GetResponseWithRedirects()
176176
177177 for ( retries = 0 ; ; retries ++ )
178178 {
179- using ( var httpClientHandler = CreateClientHandler ( ) )
179+ var httpClientHandler = CreateClientHandler ( ) ;
180+ httpClientHandler . Credentials = credentials ;
181+
182+ using ( var httpClient = this . CreateHttpClient ( httpClientHandler ) )
180183 {
181- httpClientHandler . Credentials = credentials ;
184+ var request = CreateRequest ( url , IsPost , ContentType ) ;
182185
183- using ( var httpClient = this . CreateHttpClient ( httpClientHandler ) )
186+ if ( retries > MAX_REDIRECTS )
184187 {
185- var request = CreateRequest ( url , IsPost , ContentType ) ;
186-
187- if ( retries > MAX_REDIRECTS )
188- {
189- throw new Exception ( "too many redirects or authentication replays" ) ;
190- }
191-
192- if ( IsPost && postBuffer . Length > 0 )
193- {
194- var bufferDup = new MemoryStream ( postBuffer . GetBuffer ( ) , 0 , ( int ) postBuffer . Length ) ;
188+ throw new Exception ( "too many redirects or authentication replays" ) ;
189+ }
195190
196- request . Content = new StreamContent ( bufferDup ) ;
197- request . Content . Headers . Add ( "Content-Type" , ContentType ) ;
198- }
191+ if ( IsPost && postBuffer . Length > 0 )
192+ {
193+ var bufferDup = new MemoryStream ( postBuffer . GetBuffer ( ) , 0 , ( int ) postBuffer . Length ) ;
199194
200- var response = httpClient . SendAsync ( request , HttpCompletionOption . ResponseContentRead ) . GetAwaiter ( ) . GetResult ( ) ;
195+ request . Content = new StreamContent ( bufferDup ) ;
196+ request . Content . Headers . Add ( "Content-Type" , ContentType ) ;
197+ }
201198
202- if ( response . StatusCode == HttpStatusCode . OK )
203- {
204- return response ;
205- }
206- else if ( response . StatusCode == HttpStatusCode . Unauthorized )
207- {
208- Credentials cred ;
209- int ret = SmartTransport . AcquireCredentials ( out cred , null , typeof ( UsernamePasswordCredentials ) ) ;
199+ var response = httpClient . SendAsync ( request , HttpCompletionOption . ResponseContentRead ) . GetAwaiter ( ) . GetResult ( ) ;
210200
211- if ( ret != 0 )
212- {
213- throw new InvalidOperationException ( "authentication cancelled" ) ;
214- }
201+ if ( response . StatusCode == HttpStatusCode . OK )
202+ {
203+ return response ;
204+ }
205+ else if ( response . StatusCode == HttpStatusCode . Unauthorized )
206+ {
207+ Credentials cred ;
208+ int ret = SmartTransport . AcquireCredentials ( out cred , null , typeof ( UsernamePasswordCredentials ) ) ;
215209
216- UsernamePasswordCredentials userpass = ( UsernamePasswordCredentials ) cred ;
217- credentials = new NetworkCredential ( userpass . Username , userpass . Password ) ;
218- continue ;
219- }
220- else if ( response . StatusCode == HttpStatusCode . Moved || response . StatusCode == HttpStatusCode . Redirect )
210+ if ( ret != 0 )
221211 {
222- url = new Uri ( response . Headers . GetValues ( "Location" ) . First ( ) ) ;
223- continue ;
212+ throw new InvalidOperationException ( "authentication cancelled" ) ;
224213 }
225214
226- throw new Exception ( string . Format ( "unexpected HTTP response: {0}" , response . StatusCode ) ) ;
215+ UsernamePasswordCredentials userpass = ( UsernamePasswordCredentials ) cred ;
216+ credentials = new NetworkCredential ( userpass . Username , userpass . Password ) ;
217+ continue ;
227218 }
219+ else if ( response . StatusCode == HttpStatusCode . Moved || response . StatusCode == HttpStatusCode . Redirect )
220+ {
221+ url = new Uri ( response . Headers . GetValues ( "Location" ) . First ( ) ) ;
222+ continue ;
223+ }
224+
225+ throw new Exception ( string . Format ( "unexpected HTTP response: {0}" , response . StatusCode ) ) ;
228226 }
229227 }
230228
0 commit comments