🎨 优化响应流读取,兼容MemoryStream

This commit is contained in:
LemonNoCry 2023-11-11 15:57:56 +08:00
parent afb9a0d9f5
commit c85f51a901
No known key found for this signature in database

View File

@ -11,25 +11,25 @@ public static class HttpResponseExceptions
{ {
if (response is null) if (response is null)
{ {
return default; return string.Empty;
} }
if (response.Body is FluentHttpResponseStream responseBody) //原始HttpResponseStream 无法读取
//实际上只是个包装类,内部使用了HttpResponsePipeWriter write
switch (response.Body)
{
case FluentHttpResponseStream:
case MemoryStream:
{ {
response.Body.Position = 0; response.Body.Position = 0;
//不关闭底层流 using var stream = new StreamReader(response.Body, leaveOpen: true);
using StreamReader stream = new StreamReader(responseBody, leaveOpen: true); var body = stream.ReadToEnd();
string body = stream.ReadToEnd();
response.Body.Position = 0; response.Body.Position = 0;
return body; return body;
} }
else default:
{ // throw new ApplicationException("The response body is not a FluentHttpResponseStream");
//原始HttpResponseStream 无法读取 return string.Empty;
//实际上只是个包装类,内部使用了HttpResponsePipeWriter write }
throw new ApplicationException("The response body is not a FluentHttpResponseStream");
}
return default;
} }
} }