-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCompressedContent.cs
More file actions
45 lines (37 loc) · 1.16 KB
/
CompressedContent.cs
File metadata and controls
45 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace WowthingAutoUploader;
// https://programmer.help/blogs/httpclient-and-aps.net-web-api-compression-and-decompression-of-request-content.html
public class CompressedContent : HttpContent
{
private readonly HttpContent _originalContent;
public CompressedContent(HttpContent content)
{
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}
_originalContent = content;
foreach (var header in _originalContent.Headers)
{
Headers.TryAddWithoutValidation(header.Key, header.Value);
}
Headers.ContentEncoding.Add("gzip");
}
protected override bool TryComputeLength(out long length)
{
length = -1;
return false;
}
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
using (var gzipStream = new GZipStream(stream, CompressionMode.Compress, true))
{
await _originalContent.CopyToAsync(gzipStream);
}
}
}