• 大小: 4.32MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-09-19
  • 语言: 其他
  • 标签: libcurl  例子  

资源简介

http://blog.csdn.net/infoworld/article/details/46646933 使用libcurl下载大文件简单例子.

资源截图

代码片段和文件信息


#include “stdafx.h“

#include “http_download_domain.h“

#include 

HttpDownloadDomain::HttpDownloadDomain(bool* cancel)
{
cancel_ = cancel;
}

HttpDownloadDomain::~HttpDownloadDomain()
{
}

size_t HttpDownloadDomain::DownloadCallback(void* pBuffer size_t nSize size_t nMemByte void* pParam)  
{  
    FILE* fp = (FILE*)pParam;  
    size_t nWrite = fwrite(pBuffer nSize nMemByte fp);  

    return nWrite;  
}  
  
int HttpDownloadDomain::ProgressCallback(void *clientp double dltotal double dlnow double ultotal double ulnow)  
{  
HttpDownloadDomain* dd = (HttpDownloadDomain*)clientp;

    if ( dltotal > -0.1 && dltotal < 0.1 )  
    {
return 0;
}
    int nPos = (int) ( (dlnow/dltotal)*100 );  
    //通知进度条更新下载进度
    std::cout << “dltotal: “ << (long)dltotal << “ ---- dlnow:“ << (long)dlnow << std::endl;

if(*dd->cancel_)
{
//1. 返回非0值就会终止 curl_easy_perform 执行
return -2;
}
    return 0;  
}

bool HttpDownloadDomain::DownloadFile(std::string URLADDRstd::wstring path)
{
//初始化curl,这个是必须的  
    CURL *curl = curl_easy_init();  
    curl_easy_setopt(curl CURLOPT_URL URLADDR.c_str());  

    //设置接收数据的回调 
FILE* file = _wfopen(path.c_str() L“wb“);
    curl_easy_setopt(curl CURLOPT_WRITEFUNCTION DownloadCallback);
curl_easy_setopt(curl CURLOPT_WRITEDATAfile);
    curl_easy_setopt(curl CURLOPT_MAXREDIRS 5);  
    curl_easy_setopt(curl CURLOPT_FOLLOWLOCATION 1);  
    curl_easy_setopt(curl CURLOPT_NOPROGRESS 0);  
    curl_easy_setopt(curl CURLOPT_PROGRESSFUNCTION ProgressCallback);
curl_easy_setopt(curl CURLOPT_PROGRESSDATAthis);
    CURLcode retcCode = curl_easy_perform(curl);


    const char* pError = curl_easy_strerror(retcCode);
std::cout << “pError: “ << pError << std::endl;
fclose(file);
    //清理curl,和前面的初始化匹配  
    curl_easy_cleanup(curl);

return !retcCode;
}


 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-06-26 10:46  test_curl\lib\
     文件     4223007  2015-06-24 19:17  test_curl\lib\curl-7.28.1.zip
     目录           0  2015-06-26 10:46  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\
     目录           0  2015-06-26 10:49  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\bin\
     文件      239616  2015-06-23 23:07  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\bin\curl.exe
     文件      269312  2015-06-23 23:07  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\bin\libcurl.dll
     目录           0  2015-06-26 10:46  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\include\
     目录           0  2015-06-26 10:46  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\include\curl\
     文件       83928  2012-09-26 17:46  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\include\curl\curl.h
     文件       22192  2012-11-20 15:13  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\include\curl\curlbuild.h
     文件        8901  2011-03-19 23:17  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\include\curl\curlrules.h
     文件        2741  2012-11-20 15:13  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\include\curl\curlver.h
     文件        3472  2011-11-05 06:32  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\include\curl\easy.h
     文件        2790  2011-03-19 23:16  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\include\curl\mprintf.h
     文件       13836  2012-09-17 05:35  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\include\curl\multi.h
     文件        1330  2011-03-19 23:16  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\include\curl\stdcheaders.h
     文件       36918  2012-04-25 23:29  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\include\curl\typecheck-gcc.h
     目录           0  2015-06-26 10:46  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\lib\
     文件        8053  2015-06-23 23:07  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\lib\libcurl.exp
     文件       13900  2015-06-23 23:07  test_curl\lib\libcurl-vc10-x86-release-dll-ipv6-sspi-spnego-winssl\lib\libcurl.lib
     文件         888  2015-06-23 10:23  test_curl\test_gg.sln
     文件       74752  2015-06-26 10:52  test_curl\test_gg.suo
     目录           0  2015-06-26 10:52  test_curl\test_gg\
     文件        1966  2015-06-25 19:00  test_curl\test_gg\http_download_domain.cpp
     文件         515  2015-06-25 18:54  test_curl\test_gg\http_download_domain.h
     文件        1553  2015-06-23 10:23  test_curl\test_gg\ReadMe.txt
     文件         212  2015-06-23 10:23  test_curl\test_gg\stdafx.cpp
     文件         233  2015-06-23 10:23  test_curl\test_gg\stdafx.h
     文件         236  2015-06-23 10:23  test_curl\test_gg\targetver.h
     文件         377  2015-06-26 10:22  test_curl\test_gg\test_curl.cpp
     文件        5034  2015-06-26 10:50  test_curl\test_gg\test_gg.vcxproj
............此处省略2个文件信息

评论

共有 条评论