forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResponseExceptions.cs
More file actions
35 lines (32 loc) · 1.02 KB
/
HttpResponseExceptions.cs
File metadata and controls
35 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.IO;
using Blog.Core.Common.Https;
using Microsoft.AspNetCore.Http;
namespace Blog.Core.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;
}
}
}