@@ -215,11 +215,22 @@ public virtual SDataResponse GetResponse()
215215 attempts -- ;
216216 continue ;
217217 }
218- if ( ex . Status == WebExceptionStatus . ProtocolError &&
219- ( ( HttpWebResponse ) ex . Response ) . StatusCode == HttpStatusCode . Unauthorized &&
220- Authenticator != null && Authenticator . Unauthorized ( ex . Response ) == UnauthorizedAction . Retry )
218+ if ( ex . Status == WebExceptionStatus . ProtocolError && Authenticator != null )
221219 {
222- continue ;
220+ if ( ( ( HttpWebResponse ) ex . Response ) . StatusCode == HttpStatusCode . Unauthorized )
221+ {
222+ //401
223+ if ( Authenticator . Unauthorized ( ex . Response ) == UnauthorizedAction . Retry ) continue ;
224+ }
225+ else if ( ( ( HttpWebResponse ) ex . Response ) . StatusCode == HttpStatusCode . Forbidden )
226+ {
227+ IAuthenticator2 vAuthenticator2 = Authenticator as IAuthenticator2 ;
228+ if ( vAuthenticator2 != null )
229+ {
230+ //403
231+ if ( vAuthenticator2 . Forbidden ( ex . Response ) == UnauthorizedAction . Retry ) continue ;
232+ }
233+ }
223234 }
224235 throw new SDataException ( ex ) ;
225236 }
@@ -257,87 +268,87 @@ public virtual IAsyncResult BeginGetResponse(AsyncCallback callback, object stat
257268 Action getResponse = null ;
258269 Action loop =
259270 ( ) =>
271+ {
272+ MediaType ? contentType ;
273+ object content ;
274+ _request = CreateRequest ( uri , out contentType , out content ) ;
275+ if ( content != null || Form . Count > 0 || Files . Count > 0 )
260276 {
261- MediaType ? contentType ;
262- object content ;
263- _request = CreateRequest ( uri , out contentType , out content ) ;
264- if ( content != null || Form . Count > 0 || Files . Count > 0 )
265- {
266- _request . BeginGetRequestStream (
267- async =>
277+ _request . BeginGetRequestStream (
278+ async =>
279+ {
280+ try
281+ {
282+ using ( var stream = _request . EndGetRequestStream ( async ) )
268283 {
269- try
270- {
271- using ( var stream = _request . EndGetRequestStream ( async ) )
272- {
273- WriteRequestContent( contentType, content , stream ) ;
274- }
275- TraceRequest( _request) ;
276- getResponse( ) ;
277- }
278- catch ( Exception ex )
279- {
280- result . Failure ( ex , async . CompletedSynchronously ) ;
281- _request = null ;
282- Interlocked. Exchange ( ref _state , 0 ) ;
283- }
284- } , null ) ;
285- }
286- else
287- {
288- TraceRequest ( _request ) ;
289- getResponse ( ) ;
290- }
291- } ;
284+ WriteRequestContent( contentType, content , stream ) ;
285+ }
286+ TraceRequest( _request) ;
287+ getResponse( ) ;
288+ }
289+ catch ( Exception ex )
290+ {
291+ result . Failure ( ex , async . CompletedSynchronously ) ;
292+ _request = null ;
293+ Interlocked. Exchange ( ref _state , 0 ) ;
294+ }
295+ } , null ) ;
296+ }
297+ else
298+ {
299+ TraceRequest ( _request ) ;
300+ getResponse ( ) ;
301+ }
302+ } ;
292303 getResponse =
293304 ( ) => _request . BeginGetResponse (
294305 async =>
306+ {
307+ try
295308 {
309+ WebResponse response ;
296310 try
297311 {
298- WebResponse response ;
299- try
300- {
301- response = _request . EndGetResponse ( async ) ;
302- TraceResponse( response ) ;
303- }
304- catch ( WebException webEx )
305- {
312+ response = _request . EndGetResponse ( async ) ;
313+ TraceResponse( response ) ;
314+ }
315+ catch ( WebException webEx )
316+ {
306317#if PCL || NETFX_CORE || SILVERLIGHT
307318 if ( attempts > 0 )
308319#else
309- if ( webEx . Status == WebExceptionStatus . Timeout && attempts > 0 )
320+ if ( webEx . Status == WebExceptionStatus . Timeout && attempts > 0 )
310321#endif
311- {
312- attempts -- ;
313- loop ( ) ;
314- return ;
315- }
316- throw new SDataException ( webEx ) ;
317- }
318-
319- var httpResponse = response as HttpWebResponse ;
320- var statusCode = httpResponse != null ? httpResponse . StatusCode : 0 ;
321-
322- if ( statusCode ! = HttpStatusCode . Found )
323322 {
324- result . Success ( new SDataResponse ( response, location ) , async . CompletedSynchronously ) ;
325- _request = null ;
326- Interlocked. Exchange ( ref _state , 0 ) ;
327- }
328- else
329- {
330- uri = location = response . Headers [ "Location" ] ;
323+ attempts -- ;
331324 loop ( ) ;
325+ return ;
332326 }
327+ throw new SDataException ( webEx ) ;
333328 }
334- catch ( Exception ex )
329+
330+ var httpResponse = response as HttpWebResponse ;
331+ var statusCode = httpResponse != null ? httpResponse . StatusCode : 0 ;
332+
333+ if ( statusCode ! = HttpStatusCode . Found )
335334 {
336- result . Failure ( ex , async . CompletedSynchronously ) ;
335+ result . Success ( new SDataResponse ( response , location ) , async . CompletedSynchronously ) ;
337336 _request = null ;
338337 Interlocked. Exchange ( ref _state , 0 ) ;
339338 }
340- } , null ) ;
339+ else
340+ {
341+ uri = location = response . Headers [ "Location" ] ;
342+ loop( ) ;
343+ }
344+ }
345+ catch ( Exception ex )
346+ {
347+ result . Failure ( ex , async . CompletedSynchronously ) ;
348+ _request = null ;
349+ Interlocked. Exchange ( ref _state , 0 ) ;
350+ }
351+ } , null ) ;
341352 try
342353 {
343354 loop( ) ;
@@ -356,7 +367,7 @@ public virtual SDataResponse EndGetResponse(IAsyncResult asyncResult)
356367 Guard . ArgumentIsType < AsyncResult < SDataResponse > > ( asyncResult , "asyncResult" ) ;
357368 try
358369 {
359- return ( ( AsyncResult < SDataResponse > ) asyncResult ) . End ( ) ;
370+ return ( ( AsyncResult < SDataResponse > ) asyncResult ) . End ( ) ;
360371 }
361372 finally
362373 {
@@ -381,7 +392,7 @@ private WebRequest CreateRequest(string uri, out MediaType? contentType, out obj
381392 methodOverride = true;
382393 contentType = MediaType. Text ;
383394 content = uri ;
384- uri = new SDataUri ( uri ) { Query = null } . ToString ( ) ;
395+ uri = new SDataUri ( uri ) { Query = null } . ToString ( ) ;
385396 }
386397 else
387398 {
@@ -531,18 +542,18 @@ private void WriteRequestContent(MediaType? contentType, object content, Stream
531542 {
532543 if ( contentType != null )
533544 {
534- var part = new MimePart( requestStream ) { ContentType = MediaTypeNames. GetMediaType ( contentType . Value ) } ;
545+ var part = new MimePart( requestStream ) { ContentType = MediaTypeNames. GetMediaType ( contentType . Value ) } ;
535546 multipart. Add ( part ) ;
536547 }
537548
538549 foreach ( var data in Form)
539550 {
540551 var part = new MimePart ( new MemoryStream ( Encoding . UTF8 . GetBytes ( data . Value ) ) )
541- {
542- ContentType = MediaTypeNames . TextMediaType ,
543- ContentTransferEncoding = "binary" ,
544- ContentDisposition = "inline; name=" + data . Key
545- } ;
552+ {
553+ ContentType = MediaTypeNames . TextMediaType ,
554+ ContentTransferEncoding = "binary" ,
555+ ContentDisposition = "inline; name=" + data . Key
556+ } ;
546557 multipart. Add ( part ) ;
547558 }
548559
@@ -557,11 +568,11 @@ private void WriteRequestContent(MediaType? contentType, object content, Stream
557568 }
558569
559570 var part = new MimePart ( file . Stream )
560- {
561- ContentType = file . ContentType ?? "application/octet-stream" ,
562- ContentTransferEncoding = "binary" ,
563- ContentDisposition = contentDisposition
564- } ;
571+ {
572+ ContentType = file . ContentType ?? "application/octet-stream" ,
573+ ContentTransferEncoding = "binary" ,
574+ ContentDisposition = contentDisposition
575+ } ;
565576 multipart . Add ( part ) ;
566577 }
567578
0 commit comments