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 6
6
using System . IO ;
7
7
using System . Net ;
8
8
using System . Text ;
9
+ using System . Threading ;
9
10
using System . Threading . Tasks ;
10
11
using ServiceStack . Text ;
11
12
@@ -935,7 +936,18 @@ public static string GetResponseBody(this Exception ex)
935
936
return null ;
936
937
937
938
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 ( ) ;
939
951
}
940
952
941
953
public static string ReadToEnd ( this WebResponse webRes )
You can’t perform that action at this time.
0 commit comments