我创建了一个使用 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/