Last active
January 2, 2016 13:09
-
-
Save adamwitko/8307746 to your computer and use it in GitHub Desktop.
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
public class DecompressionHandler : DelegatingHandler | |
{ | |
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
if (request.Content.Headers.ContentEncoding.Select(e => e.ToLowerInvariant()).Contains("gzip")) | |
{ | |
var inputStream = await request.Content.ReadAsStreamAsync(); | |
using (var memoryStream = new MemoryStream()) | |
{ | |
using (var outputStream = new GZipStream(inputStream, CompressionMode.Decompress, leaveOpen: true)) | |
{ | |
inputStream.CopyTo(memoryStream); | |
memoryStream.Position = 0; | |
var newContent = new StreamContent(memoryStream); | |
newContent.Headers.ContentType = request.Content.Headers.ContentType; | |
request.Content = newContent; | |
} | |
} | |
} | |
return await base.SendAsync(request, cancellationToken);; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment