Skip to content

Instantly share code, notes, and snippets.

@MiloszKrajewski
Created July 10, 2018 12:29
Show Gist options
  • Select an option

  • Save MiloszKrajewski/e8eea5e87afbf86ebbe2f1be3b823d4e to your computer and use it in GitHub Desktop.

Select an option

Save MiloszKrajewski/e8eea5e87afbf86ebbe2f1be3b823d4e to your computer and use it in GitHub Desktop.
Close HttpRequest
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