mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-07-15 23:13:32 +08:00
feat: 优化反序列化响应的判断逻辑
This commit is contained in:
parent
f9eba5ff05
commit
b50c216fab
@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
@ -121,14 +121,13 @@ namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatAdsResponse, new()
|
||||
{
|
||||
string contentType = flurlResponse.Headers.GetAll("Content-Type").FirstOrDefault() ?? string.Empty;
|
||||
string contentDisposition = flurlResponse.Headers.GetAll("Content-Disposition").FirstOrDefault() ?? string.Empty;
|
||||
bool contentTypeIsNotJson =
|
||||
(flurlResponse.StatusCode != (int)HttpStatusCode.OK) ||
|
||||
(!contentType.StartsWith("application/json") && !contentType.StartsWith("text/json")) ||
|
||||
(contentDisposition.StartsWith("attachment"));
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable = bytes.Length > 1 &&
|
||||
(bytes[0] == 91 && bytes[bytes.Length - 1] == 93) || // "[...]"
|
||||
(bytes[0] == 123 && bytes[bytes.Length - 1] == 125); // "{...}"
|
||||
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
|
||||
T result = contentTypeIsNotJson ? new T() : await flurlResponse.GetJsonAsync<T>().ConfigureAwait(false);
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
flurlResponse.Headers
|
||||
@ -138,7 +137,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Ads
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = await flurlResponse.ResponseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||
result.RawBytes = bytes;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
@ -110,12 +110,13 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatApiResponse, new()
|
||||
{
|
||||
string contentType = flurlResponse.Headers.GetAll("Content-Type").FirstOrDefault() ?? string.Empty;
|
||||
bool contentTypeIsNotJson =
|
||||
(flurlResponse.StatusCode != (int)HttpStatusCode.OK) ||
|
||||
(!contentType.StartsWith("application/json") && !contentType.StartsWith("text/json") && !contentType.StartsWith("text/plain"));
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable = bytes.Length > 1 &&
|
||||
(bytes[0] == 91 && bytes[bytes.Length - 1] == 93) || // "[...]"
|
||||
(bytes[0] == 123 && bytes[bytes.Length - 1] == 125); // "{...}"
|
||||
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
|
||||
T result = contentTypeIsNotJson ? new T() : await flurlResponse.GetJsonAsync<T>().ConfigureAwait(false);
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
flurlResponse.Headers
|
||||
@ -125,7 +126,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = await flurlResponse.ResponseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||
result.RawBytes = bytes;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
@ -150,12 +150,13 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatOpenAIPlatformResponse, new()
|
||||
{
|
||||
string contentType = flurlResponse.Headers.GetAll("Content-Type").FirstOrDefault() ?? string.Empty;
|
||||
bool contentTypeIsNotJson =
|
||||
(flurlResponse.StatusCode != (int)HttpStatusCode.OK) ||
|
||||
(!contentType.StartsWith("application/json") && !contentType.StartsWith("text/json"));
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable = bytes.Length > 1 &&
|
||||
(bytes[0] == 91 && bytes[bytes.Length - 1] == 93) || // "[...]"
|
||||
(bytes[0] == 123 && bytes[bytes.Length - 1] == 125); // "{...}"
|
||||
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
|
||||
T result = contentTypeIsNotJson ? new T() : await flurlResponse.GetJsonAsync<T>().ConfigureAwait(false);
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
flurlResponse.Headers
|
||||
@ -165,7 +166,8 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = await flurlResponse.ResponseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||
result.RawBytes = bytes;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
@ -127,12 +127,13 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatOpenAIThirdPartyResponse, new()
|
||||
{
|
||||
string contentType = flurlResponse.Headers.GetAll("Content-Type").FirstOrDefault() ?? string.Empty;
|
||||
bool contentTypeIsNotJson =
|
||||
(flurlResponse.StatusCode != (int)HttpStatusCode.OK) ||
|
||||
(!contentType.StartsWith("application/json") && !contentType.StartsWith("text/json"));
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable = bytes.Length > 1 &&
|
||||
(bytes[0] == 91 && bytes[bytes.Length - 1] == 93) || // "[...]"
|
||||
(bytes[0] == 123 && bytes[bytes.Length - 1] == 125); // "{...}"
|
||||
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
|
||||
T result = contentTypeIsNotJson ? new T() : await flurlResponse.GetJsonAsync<T>().ConfigureAwait(false);
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
flurlResponse.Headers
|
||||
@ -142,7 +143,8 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = await flurlResponse.ResponseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||
result.RawBytes = bytes;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
@ -149,11 +148,10 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatTenpayResponse, new()
|
||||
{
|
||||
string mediaType = flurlResponse.Headers.GetAll("Content-Type").FirstOrDefault() ?? "application/octet-stream";
|
||||
bool jsonable = (flurlResponse.StatusCode != (int)HttpStatusCode.NoContent) &&
|
||||
(mediaType.StartsWith("application/json") || !mediaType.StartsWith("text/json"));
|
||||
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable = bytes.Length > 1 &&
|
||||
(bytes[0] == 91 && bytes[bytes.Length - 1] == 93) || // "[...]"
|
||||
(bytes[0] == 123 && bytes[bytes.Length - 1] == 125); // "{...}"
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
|
@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http;
|
||||
@ -111,14 +111,13 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
|
||||
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
|
||||
where T : WechatWorkResponse, new()
|
||||
{
|
||||
string contentType = flurlResponse.Headers.GetAll("Content-Type").FirstOrDefault() ?? string.Empty;
|
||||
string contentDisposition = flurlResponse.Headers.GetAll("Content-Disposition").FirstOrDefault() ?? string.Empty;
|
||||
bool contentTypeIsNotJson =
|
||||
(flurlResponse.StatusCode != (int)HttpStatusCode.OK) ||
|
||||
(!contentType.StartsWith("application/json") && !contentType.StartsWith("text/json") && !contentType.StartsWith("text/plain")) ||
|
||||
(contentDisposition.StartsWith("attachment"));
|
||||
byte[] bytes = await flurlResponse.GetBytesAsync().ConfigureAwait(false);
|
||||
bool jsonable = bytes.Length > 1 &&
|
||||
(bytes[0] == 91 && bytes[bytes.Length - 1] == 93) || // "[...]"
|
||||
(bytes[0] == 123 && bytes[bytes.Length - 1] == 125); // "{...}"
|
||||
|
||||
T result = jsonable ? JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes)) : new T();
|
||||
|
||||
T result = contentTypeIsNotJson ? new T() : await flurlResponse.GetJsonAsync<T>().ConfigureAwait(false);
|
||||
result.RawStatus = flurlResponse.StatusCode;
|
||||
result.RawHeaders = new ReadOnlyDictionary<string, string>(
|
||||
flurlResponse.Headers
|
||||
@ -128,7 +127,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
|
||||
v => string.Join(", ", v.Select(e => e.Value))
|
||||
)
|
||||
);
|
||||
result.RawBytes = await flurlResponse.ResponseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||
result.RawBytes = bytes;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user