Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit c8923be

Browse files
committed
Add GetResponseBodyAsync
1 parent 6f46ce0 commit c8923be

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/ServiceStack.Text/HttpUtils.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Net;
88
using System.Text;
9+
using System.Threading;
910
using System.Threading.Tasks;
1011
using 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)

0 commit comments

Comments
 (0)