Search code examples
javaandroidkotlindownloadcontent-length

Why some servers return different content-length for the same file?


I'am developing an Android DownloadManager app and I've faced with a condition on which some servers like Linkedin server send different content-length on their response header for the same file. For example consider the link below which represents an image on the Linkedin:

https://media-exp1.licdn.com/dms/image/C5622AQH-gfIVQhjzUw/feedshare-shrink_2048_1536/0/1615695432381?e=1619049600&v=beta&t=-zbenzxOleARjnxVRMlsX-_6gmlvhzU0-2M-peUHLyI

When I send an HEAD request(using POSTMAN) to get the image's information, sometimes Content-Length is 77241 and sometimes is 79191. I know that the actual size of the image is 79191 bytes but I don't know why it sends 77241 in most cases.

Any help would be greatly appreciated.


Solution

  • I don't know that how this was happening but the problem was solved(atleast until now) by using another strategy to get the size of the file.

    What I used to do was to send a HEAD request to server and read its Content-Length. But as I mentioned the content-length sent was wrong sometimes. So I sent a GET request with the Range header equal to 0-255 and I checked that if Content-Range header is present in the the response headers or not. If it is present, I extracted the file size from it because its value's format is like this: bytes 0-255/79191 and 79191 is the correct file size.

    If Content-Range is not present, I send a HEAD request without Range header and get the file size from Content-Length header. I think in this way the file size is much reliable. Am I wrong?