File tree Expand file tree Collapse file tree 5 files changed +46
-8
lines changed
WebApiClientCore.Benchmarks/Requests
WebApiClientCore.Test/Attributes/ParameterAttributes Expand file tree Collapse file tree 5 files changed +46
-8
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1- using Microsoft . Extensions . DependencyInjection ;
2- using System ;
1+ using System ;
32using System . Linq ;
43using System . Net . Http ;
54using 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 > ( ) ;
Original file line number Diff line number Diff line change 11using System ;
22using System . Diagnostics . CodeAnalysis ;
3+ using System . Net . Http ;
34using System . Threading . Tasks ;
45
56namespace 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>
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments