ContentLength vs Actual File.Length
Does anyone has any experie开发者_StackOverflownce wherein the requested file to download (HTTP) content length in the header does not equal to the actual file length (size) when downloaded?
The content length header is the number of bytes in the body of the HTTP response.
This is calculated after all encoding stages, most encoding methods will change the length.
- Compression will shrink it
- Base 64 will increase it.
The content length header is only useful in terms of how much raw data to read from the socket. It will not help will allocating a buffer to hold the decoded content.
(I have just written some code to pull data down, but have to read the response stream incrementally expanding the buffer rather than one big allocate an read.)
The way you phrase the question is misleading.
When an HTTP response carries a content-length header, that is the length of the message. Period. Well, except for HEAD responses.
If a server sends more than that, it's broken.
You can use WebClient.DownloadTaskAsync(...)
instead. There file size will be the same.
精彩评论