Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET
Technical Blog

Don't (blindly) trust in HttpWebResponse.ContentLength

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
5 Jan 2013CPOL1 min read 10.1K   2   1
Don't (blindly) trust in HttpWebResponse.ContentLength.
Happy new year to all of you, it's been a long time since I have not posted on my blog.

While doing some HTTP web requests few days ago,I remained stuck with a strange HttpWebResponse.ContentLength == -1 issue with chunked data beyond a certain length.

My searches on the Web didn't give me a clear solution (or I didn't find / see it), so after some tests with Fiddler2, I think I was able to infer an explication, although I can't say I have the answer.

Here is one of the HTTP reponses with a correct Content-Length:
HTTP/1.1 200 OK
Date: Tue, 01 Jan 2013 21:48:42 GMT
Server: Apache
Content-Disposition: attachment; filename="*****.***"
Content-Length: 1537
Content-Type: application/***
Content-Language: fr-FR
Via: 1.1 ***.**********.**
X-Cache: MISS from ***.**********.**
Keep-Alive: timeout=15, max=90
Connection: Keep-Alive
And one with a "corrupted" Content-Length :
HTTP/1.1 200 OK
Date: Tue, 01 Jan 2013 21:48:44 GMT
Server: Apache
Content-Disposition: attachment; filename="*****.***"
Content-Type: application/***
Content-Language: fr-FR
Via: 1.1 ***.**********.**
X-Cache: MISS from ***.**********.**
Keep-Alive: timeout=15, max=86
Connection: Keep-Alive
Content-Length: 13356


I don't know (and don't want to know) why the HTTP server is not returning the headers in the same order each time (caused by chunked data ?), but I suspect that the .NET Framework (4.0) ignore the Content-Length header when it appears after the Connection one (highlighted in the above HTTP responses).

I've just decided to ignore the Content-Length header, letting the MemoryStream dynamically grow, and it works like a charm.

Better idea or suggestion?

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Stambia
France France
I'm a 39 year old team deputy CTO, living and working near Lyon (France)

I started to write softwares in the end of 90's, when I was a teenager.

I acquired several but complementary skills, mainly (but not only) on Microsoft's technologies and platforms : assembly (x86, 68k), C, C++, .NET/C#, JavaScript/HTML/CSS, PHP, DBMS (MySQL, Oracle, SQL Server, etc.), etc.

During 2007-2012, I was particulary active on .NET, ASP.NET, C#, SQL Server and ORM (NHibernate) with a growing time spent on architecture, technical management, code review, etc.

Since 2013, I'm a full-time development team leader/manager at Everial, I stopped development at work but still love to develop some personnal programs during my spare time, mainly for domotic purposes. I use to play with Arduino, ESP8266/NodeCmu, Raspberry PI... running them with some C++ and .NET Core.

My hobbies are futsal, badminton, motorcycle, Formula One, domotic, gardening... and software developement.

Comments and Discussions

 
QuestionSame Issue Pin
tameee_00716-Jan-13 20:12
tameee_00716-Jan-13 20:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.