69,373
社区成员
发帖
与我相关
我的任务
分享
typedef struct _DownFile
{
std::string url;
FILE** pOfstream;
}DownParam, *PDownParam;
size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return fwrite(ptr, size, nmemb, stream);
}
int tool_progress_cb(void *clientp,
double dltotal,
double dlnow,
double ultotal,
double ulnow)
{
// std::cout << dlnow / dltotal*100<<"%"<< std::endl;
return 0;
}
void thread_func(PDownParam param)
{
CURL *curl = nullptr;
CURLcode res;
curl = curl_easy_init();
if (curl){
curl_easy_setopt(curl, CURLOPT_URL, param->url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, *param->pOfstream);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, tool_progress_cb);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 10*1000);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 10*1000);
curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);//用来在屏幕上显示对服务器相关操作返回的信息
res = curl_easy_perform(curl);
std::cout << curl_easy_strerror(res) << std::endl;
int recode = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &recode);
fclose(*param->pOfstream);
curl_easy_cleanup(curl);
curl = nullptr;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
curl_global_init(CURL_GLOBAL_WIN32);
FILE* outfile = nullptr;
std::cout << curl_version() << std::endl;
fopen_s(&outfile,"d:\\down", "wb+");
if (outfile){
DownParam param;
param.pOfstream = &outfile;
param.url = durl;
std::thread t = std::thread(thread_func, ¶m);
t.join();
}
curl_global_cleanup();
getchar();
return 0;
}
将durl define 一个下载链接