You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
969 B
C#
33 lines
969 B
C#
10 months ago
|
using Microsoft.AspNetCore.Http;
|
||
|
|
||
|
namespace DS.Module.Core.Extensions;
|
||
|
/// <summary>
|
||
|
///
|
||
|
/// </summary>
|
||
|
public static class HttpResponseExceptions
|
||
|
{
|
||
|
public static string GetResponseBody(this HttpResponse response)
|
||
|
{
|
||
|
if (response is null)
|
||
|
{
|
||
|
return string.Empty;
|
||
|
}
|
||
|
|
||
|
//原始HttpResponseStream 无法读取
|
||
|
//实际上只是个包装类,内部使用了HttpResponsePipeWriter write
|
||
|
switch (response.Body)
|
||
|
{
|
||
|
case MemoryStream:
|
||
|
{
|
||
|
response.Body.Position = 0;
|
||
|
using var stream = new StreamReader(response.Body, leaveOpen: true);
|
||
|
var body = stream.ReadToEnd();
|
||
|
response.Body.Position = 0;
|
||
|
return body;
|
||
|
}
|
||
|
default:
|
||
|
// throw new ApplicationException("The response body is not a FluentHttpResponseStream");
|
||
|
return string.Empty;
|
||
|
}
|
||
|
}
|
||
|
}
|