From 82d07846389e49c686ff7a94fc9f67abd295c6a4 Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Tue, 11 Jan 2022 14:08:56 +0100 Subject: [PATCH] Use client encoding as fallback (#1693) --- src/RestSharp/Response/ResponseHandling.cs | 4 ++-- src/RestSharp/Response/RestResponse.cs | 4 +++- src/RestSharp/RestClient.Async.cs | 1 + 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/RestSharp/Response/ResponseHandling.cs b/src/RestSharp/Response/ResponseHandling.cs index abc983b99..7b7fcdf54 100644 --- a/src/RestSharp/Response/ResponseHandling.cs +++ b/src/RestSharp/Response/ResponseHandling.cs @@ -18,9 +18,9 @@ namespace RestSharp; static class ResponseHandling { - public static string GetResponseString(this HttpResponseMessage response, byte[] bytes) { + public static string GetResponseString(this HttpResponseMessage response, byte[] bytes, Encoding clientEncoding) { var encodingString = response.Content.Headers.ContentType?.CharSet; - var encoding = encodingString != null ? TryGetEncoding(encodingString) : Encoding.Default; + var encoding = encodingString != null ? TryGetEncoding(encodingString) : clientEncoding; return encoding.GetString(bytes); Encoding TryGetEncoding(string es) { diff --git a/src/RestSharp/Response/RestResponse.cs b/src/RestSharp/Response/RestResponse.cs index 8647780ee..415d3f157 100644 --- a/src/RestSharp/Response/RestResponse.cs +++ b/src/RestSharp/Response/RestResponse.cs @@ -14,6 +14,7 @@ using System.Diagnostics; using System.Net; +using System.Text; using RestSharp.Extensions; // ReSharper disable SuggestBaseTypeForParameter @@ -61,6 +62,7 @@ public class RestResponse : RestResponseBase { internal static async Task FromHttpResponse( HttpResponseMessage httpResponse, RestRequest request, + Encoding encoding, CookieCollection cookieCollection, CancellationToken cancellationToken ) { @@ -71,7 +73,7 @@ async Task GetDefaultResponse() { using var stream = await readTask.ConfigureAwait(false); var bytes = stream == null ? null : await stream.ReadAsBytes(cancellationToken).ConfigureAwait(false); - var content = bytes == null ? null : httpResponse.GetResponseString(bytes); + var content = bytes == null ? null : httpResponse.GetResponseString(bytes, encoding); return new RestResponse { Content = content, diff --git a/src/RestSharp/RestClient.Async.cs b/src/RestSharp/RestClient.Async.cs index 2ae8af5c6..3449c9cf7 100644 --- a/src/RestSharp/RestClient.Async.cs +++ b/src/RestSharp/RestClient.Async.cs @@ -31,6 +31,7 @@ public async Task ExecuteAsync(RestRequest request, CancellationTo ? await RestResponse.FromHttpResponse( internalResponse.ResponseMessage!, request, + Options.Encoding, CookieContainer.GetCookies(internalResponse.Url), cancellationToken )