2020-08-14 07:13:28 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net.Http;
|
2020-10-25 18:54:59 +08:00
|
|
|
|
using System.Text;
|
2020-08-31 15:59:47 +08:00
|
|
|
|
using System.Text.Json;
|
2020-08-14 07:13:28 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign.core.Extensions
|
|
|
|
|
{
|
2020-08-31 15:59:47 +08:00
|
|
|
|
internal static class HttpClientExtensions
|
2020-08-14 07:13:28 +08:00
|
|
|
|
{
|
|
|
|
|
public static async Task<TValue> GetFromJsonAsync<TValue>(this HttpClient client, string requestUri, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(client));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 15:59:47 +08:00
|
|
|
|
var res = await client.GetAsync(requestUri, cancellationToken);
|
2020-08-14 07:13:28 +08:00
|
|
|
|
if (res.IsSuccessStatusCode)
|
|
|
|
|
{
|
2020-10-25 18:54:59 +08:00
|
|
|
|
var utf8Json = await res.Content.ReadAsByteArrayAsync(Encoding.UTF8);
|
|
|
|
|
return JsonSerializer.Deserialize<TValue>(utf8Json, new JsonSerializerOptions()
|
2020-08-31 15:59:47 +08:00
|
|
|
|
{
|
|
|
|
|
PropertyNameCaseInsensitive = true,
|
2020-10-25 18:54:59 +08:00
|
|
|
|
});
|
2020-08-14 07:13:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return default;
|
|
|
|
|
}
|
2020-10-25 18:54:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads as a binary array and converts to the specified encoding
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpContent"></param>
|
|
|
|
|
/// <param name="dstEncoding">The target encoding</param>
|
|
|
|
|
/// <exception cref="ArgumentException"></exception>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static async Task<byte[]> ReadAsByteArrayAsync(this HttpContent httpContent, Encoding dstEncoding)
|
|
|
|
|
{
|
|
|
|
|
var encoding = httpContent.GetEncoding();
|
|
|
|
|
var byteArray = await httpContent.ReadAsByteArrayAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return encoding.Equals(dstEncoding)
|
|
|
|
|
? byteArray
|
|
|
|
|
: Encoding.Convert(encoding, dstEncoding, byteArray);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get encoding information from <see cref="HttpContent"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpContent"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static Encoding GetEncoding(this HttpContent httpContent)
|
|
|
|
|
{
|
|
|
|
|
var charSet = httpContent.Headers.ContentType?.CharSet;
|
|
|
|
|
if (string.IsNullOrEmpty(charSet) == true)
|
|
|
|
|
{
|
|
|
|
|
return Encoding.UTF8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var span = charSet.AsSpan().TrimStart('"').TrimEnd('"');
|
|
|
|
|
if (span.Equals(Encoding.UTF8.WebName, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return Encoding.UTF8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Encoding.GetEncoding(span.ToString());
|
|
|
|
|
}
|
2020-08-14 07:13:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|