我已經上傳了一個zip
用 WinRAR 壓縮的檔案,其中包含一個 txt 檔案到我的 github 測驗存盤庫中,我如何zip
使用下載檔案curl
?
我試過了:
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
void Download(std::string url)
{
CURL *curl_handle;
static const char *pagefilename = "data.zip";
FILE *pagefile;
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* set URL to get here */
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
/* Switch on full protocol/debug output while testing */
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
/* disable progress meter, set to 0L to enable and disable debug output */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
/* open the file */
pagefile = fopen(pagefilename, "wb");
if(pagefile) {
/* write the page body to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
/* get it! */
CURLCode res;
res = curl_easy_perform(curl_handle);
/* close the header file */
fclose(pagefile);
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
return;
}
Download("https://github.com/R3uan3/test/files/9544940/test.zip");
CURLCode res
return (CURLE_OK) 但它創建了一個檔案data.zip
,0 bytes
怎么了?
uj5u.com熱心網友回復:
問題
我檢查過
curl -I https://github.com/R3uan3/test/files/9544940/test.zip
它得到
HTTP/2 302
...
location: https://objects.githubusercontent.com/github-production-rep...
換句話說:這是一個重定向,您沒有要求 libcurl 遵循重定向 - 所以您只會存盤第一個回應(它的正文為零位元組)。
修復
您可以通過將此行添加到代碼中來使 libcurl 遵循重定向:
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/506908.html