C# HttpWebResponse contentlength = -1 when file too large

牧云@^-^@ 提交于 2019-12-06 05:41:46

When contentLength returns -1, this is most likely because the response is being returned in chunked transfer encoding (or possibly http "0.9"). As such, there is no known content-length at the start of the transmission. Just read your StreamReader until the end, and you'll have everything the server sent to you.

Expected behavior - property returns content length as set by the server, so if that header is not set you get -1 (which is likely behavior for large files that are streamed from server).

HttpWebResponse.ContentLength:

Remarks: The ContentLength property contains the value of the Content-Length header returned with the response. If the Content-Length header is not set in the response, ContentLength is set to the value -1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!