How to get the length of a file without downloading the file in a cURL binary get request

后端 未结 3 710
夕颜
夕颜 2021-01-18 01:07

I want to create a cURL request in some C++ code which will get me the length of a file in a server without downloading the file. For that, I use some cURL options to tell I

相关标签:
3条回答
  • 2021-01-18 01:36

    Have you tried with CURLINFO_CONTENT_LENGTH_DOWNLOAD instead?

    0 讨论(0)
  • 2021-01-18 01:52

    need call perform()

    curl_easy_setopt(_curl_handle, CURLOPT_HEADER, 1); curl_easy_setopt(_curl_handle, CURLOPT_NOBODY, 1);

    curl_easy_perform(_curl_handle);

    curl_easy_getinfo(_curl_handle, CURLINFO_CONTENT_LENGTH_UPLOAD, &dResult);

    0 讨论(0)
  • 2021-01-18 01:58

    CURLINFO_CONTENT_LENGTH_UPLOAD is the number of bytes uploaded. You need to use CURLINFO_CONTENT_LENGTH_DOWNLOAD instead.

    Note that if the server dynamically generates the data, the length may be different when you actualy download the file versus just downloading its headers.

    Also note that if the server sends data as compressed when downloaded, there may not be any size available in the headers (if the Transfer-Encoding header is used instead of the Content-Length header), so CURLINFO_CONTENT_LENGTH_DOWNLOAD would still return -1. The only way to know the size in that situation would be to download it in full.

    0 讨论(0)
提交回复
热议问题