We have a peculiar issue here.
We have a code on the client side to download some files from the web server using HTTPWebResponse/HttpWebRequest
We are using HttpWebResponse.ContentLength
to get the size of the file...
All these days it was working fine but suddenly it started giving -1
However when we bypass the load balancer on the server it works..
Can anyone please give a fair idea on why this might be happening
Asked
Active
Viewed 1,148 times
1
2 Answers
2
HttpWebResponse.ContentLength
just reports what the server sends in the Content-Length
header. Or, it might report -1 if the server didn't send anything.
Whatever the problem is, it's with the server, not with HttpWebResponse
.
Since you said that the problem goes away if you disable the load balancer, I would suspect that at least one of the servers that gets activated by the load balancer isn't setting the header.

Jim Mischel
- 131,090
- 20
- 188
- 351
-
Thank you Jim! I use this content length to get the size of the file to update the progress bar accordingly.. however the file gets downloaded.. so i am getting a response from the server.. – SSK Mar 15 '13 at 13:29
-
Yes, the server is sending a response, but not setting the `Content-Length` header. – Jim Mischel Mar 15 '13 at 13:31
-
is there something i need to look into IIS for fixing this? – SSK Mar 15 '13 at 13:31
-
@Sathish: I'm no IIS expert, so I don't have any specifics for you. You might search for [IIS content-length load balancer] and see what comes up. Or post a question on superuser.com. – Jim Mischel Mar 15 '13 at 14:15
2
From MSDN's documentation on the HttpWebResponse.ContentLength
property:
If the Content-Length header is not set in the response, ContentLength is set to the value -1.
Simply put -- the server is not sending the Content-Length
header.

Donut
- 110,061
- 20
- 134
- 146
-
Hi Donut.. but the server is sending the response... in my case i am able to download the file from the sever.. but only the Content-Length property returns -1 – SSK Mar 15 '13 at 13:36
-
@Sathish I understand that the body of the response is being sent, but the `Content-Length` header is different (and that's what's not being sent by the server). – Donut Mar 15 '13 at 13:42
-
So how should i fix this.. is there something i need to check on the server side or IIS settings – SSK Mar 15 '13 at 13:50