Search code examples
c#azurecompressionhttpclientgzip

HttpClient Gzip Compression


I have the following code running that calls an API, decompresses the response and then converts it to an object:

 public static async Task<List<marketData>> GetAllEvents()
    {
        string res = "";

        HttpClientHandler handler = new HttpClientHandler();
        handler.AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip;

        using (HttpClient client = new HttpClient(handler))
        {
            client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");

            using (HttpResponseMessage response = await client.GetAsync("http://services.betvictor.com/omds/events/query/sport/200/markettypes/6/periods/-1.json?ep=true&e=true"))
            {                    
                Stream dataStream = await response.Content.ReadAsStreamAsync();

                using (StreamReader reader = new StreamReader(dataStream, Encoding.UTF8))
                {
                    res = await reader.ReadToEndAsync();
                }

                dataStream.Close();
                dataStream.Dispose();                 
            }
        }

        handler.Dispose();

        getEvents.Rootobject rawdata = JsonConvert.DeserializeObject<getEvents.Rootobject>(res);
        return sortMarketsAndRaces(rawdata);
    }

This all runs absolutely fine when running on localhost, but when deploying using an Azure Server I get the error:

"The archive entry was compressed using an unsupported compression method."

Stacktrace:

at System.IO.Compression.Inflater.Inflate(FlushCode flushCode) at System.IO.Compression.Inflater.ReadInflateOutput(Byte* bufPtr, Int32 length, FlushCode flushCode, Int32& bytesRead) at System.IO.Compression.Inflater.InflateVerified(Byte* bufPtr, Int32 length) at System.IO.Compression.DeflateStream.FinishReadAsyncMemory(ValueTask1 readTask, Memory1 buffer, CancellationToken cancellationToken) at System.IO.StreamReader.ReadBufferAsync() at System.IO.StreamReader.ReadToEndAsyncInternal() at tf.PriceService.Core.Classes.BetVictorMatching.GetAllEvents() in C:\Data\tf-price-service\src\tf.PriceService.Core\Classes\BetVictorMatching.cs:line 228 at tf.PriceService.Core.Services.PriceService.<>c.<b__53_6>d.MoveNext() in C:\Data\tf-price-service\src\tf.PriceService.Core\Services\PriceService.cs:line 253 --- End of stack trace from previous location where exception was thrown --- at tf.PriceService.Core.Services.PriceService.getAllBookmakerMeetingsAndRaces(DateTime date) in C:\Data\tf-price-service\src\tf.PriceService.Core\Services\PriceService.cs:line 255 at tf.PriceService.Core.Services.PriceService.processRacePrices(Object state) in C:\Data\tf-price-service\src\tf.PriceService.Core\Services\PriceService.cs:line 308


Solution

  • The problem was due to the IP of the Azure server not being on the API whitelist therefore it was returning a 403 forbidden error in the response.