简体   繁体   中英

Force To Read HttpWebResponse.GetResponseStream() When Content-Length Is Zero

As title said.

Sometime an HTTP response from remote server includes Content-Length header with zero value, but it still return HTTP body . The body can still be obtained with jQuery in browser, but using HttpWebResponse.GetResponseStream() can't read anything. How do I force to read it? Thanks everyone.

Here's an example of HTTP response which I met.


HTTP/1.1 200 OK

Connection: keep-alive

Cache-Control: no-cache,no-store

Content-Type: text/plain

Content-Length: 0

Date: Mon, 30 Sep 2013 09:34:21 GMT

(HTTP Body)

Looking through the source code I could not find a way to force this behavior. Probably there's nothing you can do.

Try disabling HTTP pipelining. Maybe HttpWebRequest does not rely on the content length in that case. It is unlikely that it will help.

Either use a different HTTP library, or send the request using TcpClient . You can use Fiddler to copy a template HTTP request and use that as the basis for yours. Should not be a lot of work.

You could also create a rewriting proxy that removes the header. That's probably a high-effort solution.

First question - Where did you get the HTTP response? Did you capture it with Fiddler or are you typing in what you think you're seeing by examining the HttpWebResponse in a debugger? I would suggest running this with Fiddler and examining the complete response headers if you didn't already.

We need to establish first if the Content-Length is being returned with a value of zero or if some of the other code in your project is causing you to interpret it as zero or to set your copy of the content length to zero.

I think one of two things is happening here:

  1. The response is not exactly as your indicated above and examining it with Fiddler will establish that quickly. Most likely I would suspect that Content-Length is actually missing and Transfer-Encoding is present and set to chunked; in that case the HttpWebResponse.ContentLength property will return -1 instead of 0 (zero).
  2. The response is exactly as you indicated (particularly in that Content-Length is present and specified as zero), in which case your web server is just busted. I shouldn't be returning a body with Content-Length set to zero; it can return no Content-Length if it was HTTP 1.0, but it clearly says HTTP 1.1 above, so it should be returning Transfer-Encoding: chunked and no Content-Length or a non-zero Content-Length if not chunking and returning a body.

Harold

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM