Skip to content

Instantly share code, notes, and snippets.

@AntonSmolkov
Last active May 9, 2021 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonSmolkov/76d22bedc40a3741060fcbf7f5b94ee7 to your computer and use it in GitHub Desktop.
Save AntonSmolkov/76d22bedc40a3741060fcbf7f5b94ee7 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static readonly HttpClient Client = new HttpClient();
public static readonly Encoding Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
private static async Task<Stream> GenerateStreamFromStringAsync(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream, Encoding);
await writer.WriteAsync(s);
await writer.FlushAsync();
stream.Position = 0;
return stream;
}
static async Task Main()
{
try
{
await using var bodyStream = new MemoryStream();
await using var gzipBodyStream = new GZipStream(bodyStream, CompressionMode.Compress);
await (await GenerateStreamFromStringAsync("{\"mydummy\": \"json\"}"))
.CopyToAsync(gzipBodyStream);
await gzipBodyStream.FlushAsync();
bodyStream.Position = 0;
var content = new StreamContent(bodyStream);
content.Headers.Add("Content-Type","application/json");
content.Headers.Add("Content-Encoding","gzip");
var response = await Client.PostAsync("http://localhost:8041", content);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
catch(HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ",e.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment