Skip to content

Commit 07ef035

Browse files
committed
修复当响应的json使用非utf8编码时反序列化的问题
1 parent 2733a73 commit 07ef035

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

WebApiClientCore.Benchmarks/Requests/GetBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public async Task<Model> HttpClient_GetAsync()
2424
var id = "id";
2525
var request = new HttpRequestMessage(HttpMethod.Get, $"http://webapiclient.com/{id}");
2626
var response = await httpClient.SendAsync(request);
27-
var json = await response.Content.ReadAsByteArrayAsync();
27+
var json = await response.Content.ReadAsUtf8ByteArrayAsync();
2828
return JsonSerializer.Deserialize<Model>(json);
2929
}
3030

WebApiClientCore.Benchmarks/Requests/PostJsonBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public async Task<Model> HttpClient_PostJsonAsync()
2929
};
3030

3131
var response = await httpClient.SendAsync(request);
32-
json = await response.Content.ReadAsByteArrayAsync();
32+
json = await response.Content.ReadAsUtf8ByteArrayAsync();
3333
return JsonSerializer.Deserialize<Model>(json);
3434
}
3535

WebApiClientCore.Test/Attributes/ParameterAttributes/JsonContentAttributeTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.Extensions.DependencyInjection;
2-
using System;
1+
using System;
32
using System.Linq;
43
using System.Net.Http;
54
using System.Threading.Tasks;
@@ -24,10 +23,10 @@ public async Task BeforeRequestAsyncTest()
2423
context.HttpContext.RequestMessage.RequestUri = new Uri("http://www.webapi.com/");
2524
context.HttpContext.RequestMessage.Method = HttpMethod.Post;
2625

27-
var attr = new JsonContentAttribute();
26+
var attr = new JsonContentAttribute() { CharSet = "utf-16" };
2827
await attr.OnRequestAsync(new ApiParameterContext(context, 0));
2928

30-
var body = await context.HttpContext.RequestMessage.Content.ReadAsByteArrayAsync();
29+
var body = await context.HttpContext.RequestMessage.Content.ReadAsUtf8ByteArrayAsync();
3130

3231
var options = context.HttpContext.HttpApiOptions.JsonSerializeOptions;
3332
using var buffer = new BufferWriter<byte>();

WebApiClientCore/ApiResponseContext.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
3+
using System.Net.Http;
34
using System.Threading.Tasks;
45

56
namespace WebApiClientCore
@@ -71,9 +72,9 @@ public ApiResponseContext(ApiRequestContext context)
7172
}
7273

7374
var serializer = this.HttpContext.ServiceProvider.GetJsonSerializer();
74-
var json = await this.HttpContext.ResponseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
75+
var utf8Json = await this.HttpContext.ResponseMessage.Content.ReadAsUtf8ByteArrayAsync().ConfigureAwait(false);
7576
var options = this.HttpContext.HttpApiOptions.JsonDeserializeOptions;
76-
return serializer.Deserialize(json, objType, options);
77+
return serializer.Deserialize(utf8Json, objType, options);
7778
}
7879

7980
/// <summary>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Text;
2+
using System.Threading.Tasks;
3+
4+
namespace System.Net.Http
5+
{
6+
/// <summary>
7+
/// HttpContent扩展
8+
/// </summary>
9+
public static class HttpContentExtensions
10+
{
11+
/// <summary>
12+
/// 读取为二进制数组并转换为utf8编码
13+
/// </summary>
14+
/// <param name="httpContent"></param>
15+
/// <returns></returns>
16+
public static Task<byte[]> ReadAsUtf8ByteArrayAsync(this HttpContent httpContent)
17+
{
18+
return httpContent.ReadAsByteArrayAsync(Encoding.UTF8);
19+
}
20+
21+
/// <summary>
22+
/// 读取为二进制数组并转换为指定的编码
23+
/// </summary>
24+
/// <param name="httpContent"></param>
25+
/// <param name="dstEncoding">目标编码</param>
26+
/// <returns></returns>
27+
public static async Task<byte[]> ReadAsByteArrayAsync(this HttpContent httpContent, Encoding dstEncoding)
28+
{
29+
var byteArray = await httpContent.ReadAsByteArrayAsync().ConfigureAwait(false);
30+
var charSet = httpContent.Headers.ContentType?.CharSet;
31+
var encoding = charSet == null ? Encoding.UTF8 : Encoding.GetEncoding(charSet);
32+
33+
return encoding.Equals(dstEncoding)
34+
? byteArray
35+
: Encoding.Convert(encoding, dstEncoding, byteArray);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)