-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Response handling improvements (#1690)
* * Handle response content with custom encoding detection * Add ConfigureAwait(false) to async calls * One more async * Configure more awaits * Event more configure await * Simplify the Get with object parameters
- Loading branch information
1 parent
1556eed
commit b2031e0
Showing
9 changed files
with
136 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
using System.Text; | ||
|
||
namespace RestSharp; | ||
|
||
static class ResponseHandling { | ||
public static string GetResponseString(this HttpResponseMessage response, byte[] bytes) { | ||
var encodingString = response.Content.Headers.ContentEncoding.FirstOrDefault(); | ||
var encoding = encodingString != null ? TryGetEncoding(encodingString) : Encoding.Default; | ||
return encoding.GetString(bytes); | ||
|
||
Encoding TryGetEncoding(string es) { | ||
try { | ||
return Encoding.GetEncoding(es); | ||
} | ||
catch { | ||
return Encoding.Default; | ||
} | ||
} | ||
} | ||
|
||
public static Task<Stream?> ReadResponse(this HttpResponseMessage response, CancellationToken cancellationToken) { | ||
#if NETSTANDARD | ||
return response.Content.ReadAsStreamAsync(); | ||
# else | ||
return response.Content.ReadAsStreamAsync(cancellationToken); | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.