Karen Avdalyan
Karen Avdalyan

Reputation: 392

Http Server not returning content-encoding header in response

I'm making http request using HttpWebRequest client

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestString);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Method = "GET"; 
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
public void GetResponseCallback(IAsyncResult asynchronousResult)
{
    HttpWebResponse response = null;
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);

    Console.WriteLine(response.Headers["Content-Encoding"]);
            
    if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Moved)
    {   
        Stream inputStream = response.GetResponseStream();
        StreamReader stream = new StreamReader(inputStream);
        string responseString = stream.ReadToEnd(); 
    }
}

On Apache settings gzip compression is enabled and working normally because when making request to the same url from chrome browser I can see content-encoding in response Headers. Please see the attached screenshot.

enter image description here

But when I make the same request using the above provided code Content-Encoding key exists in headers but it's value is empty string.

Why content-encoding value is missing in my response header? Here is the response headers string

Headers = {Connection: keep-alive  Vary: Accept-Encoding  Content-Encoding:   Access-Control-Allow-Origin: *  Content-Length: 36954  Content-Type: text/html; charset=UTF-8  Date: Fri, 18 Sep 2020 11:31:25 GMT  Server: nginx/1.10.2  X-Powered-By: PHP/5.3.3    }

Upvotes: 0

Views: 1451

Answers (1)

Karen Avdalyan
Karen Avdalyan

Reputation: 392

In case anyone else meets the same problem. In my case the prblem was in line

request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

I've changed it to

request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

It's strange to me, because my project .Net version is 4.7.1 However now it works normally.

Upvotes: 0

Related Questions