mirror of
https://github.com/anjoy8/Blog.Core.git
synced 2024-09-20 23:48:27 +08:00
🎨 优化响应流读取,兼容MemoryStream
This commit is contained in:
parent
afb9a0d9f5
commit
c85f51a901
|
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user