Skip to content

Commit

Permalink
Use client encoding as fallback (#1693)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Jan 11, 2022
1 parent c7eebdf commit 82d0784
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/RestSharp/Response/ResponseHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/RestSharp/Response/RestResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System.Diagnostics;
using System.Net;
using System.Text;
using RestSharp.Extensions;

// ReSharper disable SuggestBaseTypeForParameter
Expand Down Expand Up @@ -61,6 +62,7 @@ public class RestResponse : RestResponseBase {
internal static async Task<RestResponse> FromHttpResponse(
HttpResponseMessage httpResponse,
RestRequest request,
Encoding encoding,
CookieCollection cookieCollection,
CancellationToken cancellationToken
) {
Expand All @@ -71,7 +73,7 @@ async Task<RestResponse> 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,
Expand Down
1 change: 1 addition & 0 deletions src/RestSharp/RestClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public async Task<RestResponse> ExecuteAsync(RestRequest request, CancellationTo
? await RestResponse.FromHttpResponse(
internalResponse.ResponseMessage!,
request,
Options.Encoding,
CookieContainer.GetCookies(internalResponse.Url),
cancellationToken
)
Expand Down

0 comments on commit 82d0784

Please sign in to comment.