Last active
May 9, 2021 07:31
-
-
Save AntonSmolkov/76d22bedc40a3741060fcbf7f5b94ee7 to your computer and use it in GitHub Desktop.
This file contains 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; | |
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