如何使用 libcurl 获取文件名?

How to get file name using libcurl?

我用libcurl下载文件,url可能是下面这样

url = "https://***.com/**/***/abc"
url = "http://***:8000/**/test.txt?***2484d197c16b2e"
url = "http://***:8000/**/test.txt"

获取文件名比较麻烦,请问有没有下载的时候用libcurl获取文件名的方法呢?我的下载码在下面。

size_t writeData(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written = fwrite(ptr, size, nmemb, stream);
    return written;
}

int downloadFile(const char *url, const char *saveFile)
{
    CURL *curl;
    FILE *saveFd;
    CURLcode res;
   
    curl = curl_easy_init();
    if (curl) {
        saveFd = fopen(saveFile,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, saveFd);
        std::string strurl = string(url);
        if (strurl.find("https") != string::npos) {
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        
        fclose(saveFd);
    }
    return 0;
}

使用 CURLOPT_HEADERFUNCTION 获取每个 HTTP header 的回调。您正在寻找名为 Content-Disposition 的 header。