Created
July 10, 2018 12:29
-
-
Save MiloszKrajewski/e8eea5e87afbf86ebbe2f1be3b823d4e to your computer and use it in GitHub Desktop.
Close HttpRequest
This file contains hidden or 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
| private static async Task<HttpRequestMessage> CloneRequest(HttpRequestMessage request) | |
| { | |
| var clone = new HttpRequestMessage(request.Method, request.RequestUri); | |
| if (request.Content != null) | |
| { | |
| var stream = await request.Content.ReadAsStreamAsync(); | |
| if ((stream.Length > 0 || request.Content.Headers.Any()) | |
| && clone.Method != HttpMethod.Get) | |
| { | |
| clone.Content = new StreamContent(stream); | |
| foreach (var h in request.Content.Headers.EmptyIfNull()) | |
| clone.Content.Headers.Add(h.Key, h.Value); | |
| } | |
| } | |
| clone.Version = request.Version; | |
| foreach (var property in request.Properties) | |
| clone.Properties.Add(property); | |
| foreach (var header in request.Headers) | |
| clone.Headers.TryAddWithoutValidation(header.Key, header.Value); | |
| return clone; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment