forked from tmenier/Flurl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exception caused by automatic redirection resulting in the destructio…
…n of HttpContent in .NET Framework 4.6.x and 4.7.x is fixed. see: tmenier#644 dotnet/runtime#14612 dotnet/corefx#19082
- Loading branch information
Showing
3 changed files
with
43 additions
and
4 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,38 @@ | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Flurl.Http | ||
{ | ||
/// <summary> | ||
/// Extension methods of HttpContent | ||
/// </summary> | ||
public static class HttpContentExtensions | ||
{ | ||
/// <summary>Get a copy of the request content.</summary> | ||
/// <param name="content">The content to copy.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <remarks>Note that cloning content isn't possible after it's dispatched, because the stream is automatically disposed after the request.</remarks> | ||
internal static async Task<HttpContent> CloneAsync(this HttpContent content, CancellationToken cancellationToken = default) { | ||
if (content == null) | ||
return null; | ||
|
||
Stream stream = new MemoryStream(); | ||
await content | ||
.CopyToAsync(stream | ||
#if NET5_0_OR_GREATER | ||
, cancellationToken | ||
#endif | ||
) | ||
.ConfigureAwait(false); | ||
stream.Position = 0; | ||
|
||
StreamContent clone = new StreamContent(stream); | ||
foreach (var header in content.Headers) | ||
clone.Headers.Add(header.Key, header.Value); | ||
|
||
return clone; | ||
} | ||
} | ||
} |