2

I need to read a body of a response where a Content-Length is explicitly specified as zero. But the stream.Read does not read anything and returns 0. When the Content-Length header is not present, it reads all the bytes successfully.

When I open the web page in the browser it is empty, but I can see the contents in Fiddler.

Is there any way to get the Stream returned from GetResponseStream to read the bytes when the Content-Length is 0?

Sample response:

HTTP/1.1 200 OK
Cache-Control: public, must-revalidate
ETag: "c7f40cb26b1e95c2245f1584371465582f996a8a88b34a2cc99bbe922b1a2857"
Vary: Accept-Encoding
X-Request-Id: 4b799b6d-cd68-47f7-a392-4fd0a327f5de
X-Runtime: 0.001646
Content-Length: 0

- some content -

Simplified version of the code:

using(var stream = httpRes.GetResponseStream())
{
     while (true)
     {
          var bytesRead = stream.Read(
                            responseBuffer,
                            0,
                            responseBuffer.Length);

          // other stuff
     }
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • 1
    How did you try to read the data? `Content-Length` *shouldn't* be 0 if there are data. This could be caused by a server bug or a proxy like Fiddler that buffers chunked data and returns a single response. [Check this question](https://stackoverflow.com/questions/21613509/httpwebresponse-contentlength-always-1). One of the answers got a weird response because of Fiddler's interference. – Panagiotis Kanavos Jun 13 '19 at 12:08
  • @PanagiotisKanavos It's not because of fiddler, the Content-Type is intended to be 0 – Selman Genç Jun 13 '19 at 12:10
  • [In this question](https://stackoverflow.com/questions/9457907/httpwebrequest-invalid-contentlength) the content length and stream size didn't match because of chunked encoding. – Panagiotis Kanavos Jun 13 '19 at 12:11
  • Have you tried reading to the end of the stream with a reader? Or copying to a MemoryStream? Did you try disabling response buffering? Clearly the content length is wrong. If you google for it, you'll see that some products do return the wrong length eg when streaming data and sending 0 instead of -1. – Panagiotis Kanavos Jun 13 '19 at 12:20
  • The stream uses in HttpWebRequest/HttpWebResponse is a ConnectStream. [The read constructor](https://referencesource.microsoft.com/#system/net/System/Net/_ConnectStream.cs,253) accepts a parameter with the total number of bytes to read, which affects how the stream behaves in *non* chunked responses. Frankly, I got a headache just trying to get to that point. Apart from trying to modify the CachePolicy or `AllowReadBuffering` property (which is false by default), I don't know how you can ignore the bad content length – Panagiotis Kanavos Jun 13 '19 at 12:40
  • Have you tried using HttpClient, especially in .NET Core 2.1? It uses completely [different code and a new SocketsHttpClientHandler](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.socketshttphandler?view=netcore-2.2) – Panagiotis Kanavos Jun 13 '19 at 12:45
  • @PanagiotisKanavos haven't tried it, I will check it out. thanks for the effort :) – Selman Genç Jun 13 '19 at 12:46

1 Answers1

3

When an HTTP/1.1 has Content-Length: 0, then, by definition, there is no payload to read. If bytes follow, they by definition are the response to the subsequent request.

Now, obviously, this is not the case here. So, just to be absolutely clear: you're asking for a workaround to deal with a broken server. It would be much cleaner to fix the server instead.

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98