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.
35 lines
1.0 KiB
C#
35 lines
1.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using EntrustSettle.Common.Https;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace EntrustSettle.Common.Extensions;
|
|
|
|
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 FluentHttpResponseStream:
|
|
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;
|
|
}
|
|
}
|
|
} |