我需要一些帮助。 我正在尝试使用控制台从Google云端硬盘保存标准文件。
以下是代码:
m_pHeadersList = NULL;
int NotF; // number of the file
cout << endl << "choose file to download:"; cin >> NotF;
access_token = FileLoad ("access_token");
m_tmpStr = "Authorization: Bearer " + access_token;
m_pHeadersList = curl_slist_append(m_pHeadersList, m_tmpStr.c_str());
GETreq(curl, res, ws2s(files[NotF].downloadUrl) , m_pHeadersList);
这是GETreq函数的代码:
void GETreq(CURL *curl, CURLcode res, string URL, curl_slist* m_pHeadersList)
{
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
curl_easy_setopt(curl, CURLOPT_CAINFO , "cacert.pem");
curl_easy_setopt(curl, CURLOPT_URL, URL);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_pHeadersList);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
static const char *headerfilename1 = "head1.out";
FILE *headerfile1;
static const char *bodyfilename1 = "body1.out";
FILE *bodyfile1;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
headerfile1 = fopen(headerfilename1,"wb");
bodyfile1 = fopen(bodyfilename1,"wb");
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, headerfile1);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, bodyfile1);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
fclose(headerfile1);
fclose(bodyfile1);
curl_easy_cleanup(curl);
}
}
它可以工作,但它将文件(txt,xml甚至jpg)作为文本保存到body1.out。 我需要一些关于如何使用自己的扩展名下载文件的建议。 提前谢谢。
答案 0 :(得分:0)
处理它。刚刚更改了函数中的参数
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
到
curl_easy_setopt(curl, CURLOPT_HEADER, 0);