ContentLength of HttpResponse is negative when compression is used

0 votes
asked Sep 24, 2019 by mdisg (150 points)


why is the value of the ContentLength Property -1 if the content is compressed with gzip?

The content length received from the server informs about the length of the compressed content so it's okay that this information is being removed. But why is the caller not informed about the real decompressed content length of the response?
I would expect that ContentLength returns the length of the raw data of the response.

Is there another way to retrieve that information?

Best regards

Applies to: Rebex HTTPS
commented Sep 24, 2019 by Lukas Pokorny (126,910 points)
We will look into this! However, I'm afraid that the length of the raw data might not be available when HttpResponse is retrieved because data is received and decompressed on-the-fly when reading from the response stream. Therefore, the total length of decompressed data might not be available until its actually read.

1 Answer

0 votes
answered Sep 26, 2019 by Lukas Matyska (60,770 points)
selected Sep 30, 2019 by mdisg
Best answer

It is as Lukas Pokorny wrote in comment, the length of decompressed data is not known until whole response is decompressed. There is no HTTP header which contain such value.

I ensured that Rebex has exact same behavior as System.Net.HttpWebRequest class using and this code:

var request = (HttpWebRequest)WebRequest.Create("");
request.AutomaticDecompression = DecompressionMethods.GZip;

using (var response = request.GetResponse())
    Console.WriteLine("ContentLength: {0}", response.ContentLength);

    using (var reader = new StreamReader(response.GetResponseStream()))
        string body = reader.ReadToEnd();
        Console.WriteLine("Body length: {0}", body.Length);

    Console.WriteLine("ContentLength: {0}", response.ContentLength);

The output of this program is:

ContentLength: -1
Body length: 166
ContentLength: -1
commented Sep 30, 2019 by mdisg (150 points)
Thanks for the investigation.

I disabled the decompression at all because for my case i must know the complete content length before reading the stream.