This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +13
-1
lines changed
Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Original file line number Diff line number Diff line change 66using System . IO ;
77using System . Net ;
88using System . Text ;
9+ using System . Threading ;
910using System . Threading . Tasks ;
1011using ServiceStack . Text ;
1112
@@ -935,7 +936,18 @@ public static string GetResponseBody(this Exception ex)
935936 return null ;
936937
937938 var errorResponse = ( HttpWebResponse ) webEx . Response ;
938- return errorResponse . GetResponseStream ( ) . ReadToEnd ( UseEncoding ) ;
939+ using var responseStream = errorResponse . GetResponseStream ( ) ;
940+ return responseStream . ReadToEnd ( UseEncoding ) ;
941+ }
942+
943+ public static async Task < string > GetResponseBodyAsync ( this Exception ex , CancellationToken token = default )
944+ {
945+ if ( ! ( ex is WebException webEx ) || webEx . Response == null || webEx . Status != WebExceptionStatus . ProtocolError )
946+ return null ;
947+
948+ var errorResponse = ( HttpWebResponse ) webEx . Response ;
949+ using var responseStream = errorResponse . GetResponseStream ( ) ;
950+ return await responseStream . ReadToEndAsync ( UseEncoding ) . ConfigAwait ( ) ;
939951 }
940952
941953 public static string ReadToEnd ( this WebResponse webRes )
You can’t perform that action at this time.
0 commit comments