c++ - 使用 curl 下载文件时确定扩展名?

标签 c++ curl

我创建了一个使用 curl 从互联网下载字幕的程序
如何知道我下载的文件的扩展名(.zip 或 .rar)?

这是我的代码(它是函数的一部分)

FILE* download=fopen("download.zip","wb");//i assume it's a zip
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_POST,1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,post_data.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
curl_easy_setopt(curl, CURLOPT_URL,url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write2file);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, download);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
    std::cout<<"Download Failed "<<std::endl;
curl_easy_cleanup(curl);
fclose(download);

最佳答案

您可以使用 curl_easy_getinfo()函数,它具有内容类型信息:

CURLINFO_CONTENT_TYPE

Pass a pointer to a char pointer to receive the content-type of the downloaded object. This is the value read from the Content-Type: field. If you get NULL, it means that the server didn't send a valid Content-Type header or that the protocol used doesn't support this.

使用这些信息,您可以通过在磁盘上重命名来为下载的文件提供正确的扩展名。

关于c++ - 使用 curl 下载文件时确定扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11922493/

相关文章:

magento - Curl、Magento 和 Paypal。 (Curl 不返回任何错误)

c++ - 警告 : 'struct curl_fileinfo' declared inside parameter list

c++ - Qt - 没有使用正确的源文件

c++ - boost::scoped_ptr 指针的值到底是什么?

c++ - 使用 dynamic_cast 来验证指向多态类型的指针

php - 如何从 ajax 更改 css 值

php - 如何从 php curl 获取 cookie 到变量中

c++ - 将重载函数传递给另一个函数时如何指定重载?

c++ - 使用 foreach 遍历嵌套的 QHash

php - 使用 cURL 下载大型 XML 文件