Created
April 22, 2022 09:28
-
-
Save DominikPalo/5e29dfb42a7b7306b6d703851c63706f to your computer and use it in GitHub Desktop.
GZIP compressed HTTP content
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
using System.IO; | |
using System.IO.Compression; | |
using System.Net.Http; | |
using System.Text; | |
namespace System.Net.Http | |
{ | |
public class GzipContent : ByteArrayContent | |
{ | |
public GzipContent(string content, Encoding encoding, string mediaType) | |
: base(Compress(content, encoding)) | |
{ | |
this.Headers.Add("Content-Type", mediaType); | |
this.Headers.Add("Content-Encoding", "gzip"); | |
} | |
private static byte[] Compress(string content, Encoding encoding) | |
{ | |
using (var compressedStream = new MemoryStream()) | |
{ | |
using (var contentStream = new MemoryStream(encoding.GetBytes(content))) | |
using (var gzipStream = new GZipStream(compressedStream, CompressionMode.Compress)) | |
{ | |
contentStream.CopyTo(gzipStream); | |
} | |
return compressedStream.ToArray(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment