#define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 60000000000
#define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL 3
struct myprogress {
double lastruntime;
CURL *curl;
};
size_t write_data(char *buffer, size_t size, size_t nitems, void *outstream)
{
int written = fwrite(buffer, size, nitems, (FILE*)outstream);
return written;
}
static int xferinfo(void *p,
curl_off_t dltotal, curl_off_t dlnow,
curl_off_t ultotal, curl_off_t ulnow, int a)
{
struct myprogress *myp = (struct myprogress *)p;
CURL *curl = myp->curl;
DoEvents();
double dDowning = 0.0, dDwownTotal = 0.0;
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &dDowning);
int nRandom = rand();
if (dDowning == 0)
switch (nRandom % 7)
{
case 0:acedSetStatusBarProgressMeter(_T("正"), 0, 100); break;
case 1:acedSetStatusBarProgressMeter(_T("正在"), 0, 100); break;
case 2:acedSetStatusBarProgressMeter(_T("正在连"), 0, 100); break;
case 3:acedSetStatusBarProgressMeter(_T("正在连接"), 0, 100); break;
case 4:acedSetStatusBarProgressMeter(_T("正在连接."), 0, 100); break;
case 5:acedSetStatusBarProgressMeter(_T("正在连接.."), 0, 100); break;
case 6:acedSetStatusBarProgressMeter(_T("正在连接..."), 0, 100); break;
default:
break;
}
else if (dDowning < 2048)
acedSetStatusBarProgressMeter(_T("正在下载..."), 0, 100);
else
{
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dDwownTotal);
acedSetStatusBarProgressMeterPos(dDowning / dDwownTotal * 100);
}
if (dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
return 1;
return 0;
}
/////////////////////////////////////////////////////这是Get
int DownloadFileAndSaveToLocal(const char* szUrl, const char* szLocalPath)
{
CURL *pCurl;
FILE* pFile = fopen(szLocalPath, "wb");
pCurl = curl_easy_init();
struct myprogress prog;
prog.lastruntime = 0;
prog.curl = pCurl;
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, (void*)pFile);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(pCurl, CURLOPT_URL, szUrl);
curl_easy_setopt(pCurl, CURLOPT_XFERINFOFUNCTION, xferinfo);
curl_easy_setopt(pCurl, CURLOPT_PROGRESSDATA, &prog);
curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 0L);
acedSetStatusBarProgressMeter(_T("正在下载..."), 0, 100);
int nOK = curl_easy_perform(pCurl);
acedRestoreStatusBar();
fclose(pFile);
curl_easy_cleanup(pCurl);
return nOK;
}
/////////////////////////////////////////////////////这是Post
int DownloadFileAndSaveToLocal_Post(const char* szUrl, const char* szLocalPath, const char* szJsonData)
{
CURL *pCurl;
FILE* pFile = fopen(szLocalPath, "wb");
pCurl = curl_easy_init();
struct myprogress prog;
prog.lastruntime = 0;
prog.curl = pCurl;
CURLcode code;
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, (void*)pFile);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, write_data);
curl_slist *http_headers = NULL;
http_headers = curl_slist_append(http_headers, "Accept: application/json");
http_headers = curl_slist_append(http_headers, "Content-Type: application/json");
http_headers = curl_slist_append(http_headers, "charsets: utf-8");
curl_easy_setopt(pCurl, CURLOPT_CUSTOMREQUEST, "POST");
code = curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, http_headers);
curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(pCurl, CURLOPT_URL, szUrl);//"http://218.25.36.152:20179/api/DataInteroperability/GetMdbFile?tid=D0DD2310F638433DA70F698DCCBC600A"
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, szJsonData);
curl_easy_setopt(pCurl, CURLOPT_XFERINFOFUNCTION, xferinfo);
curl_easy_setopt(pCurl, CURLOPT_PROGRESSDATA, &prog);
curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 0L);
acedSetStatusBarProgressMeter(_T("正在下载..."), 0, 100);
int nOK = curl_easy_perform(pCurl);
acedRestoreStatusBar();
fclose(pFile);
curl_easy_cleanup(pCurl);
return nOK;
}
利用curl下载文件,包括Get和Post
猜你喜欢
转载自blog.csdn.net/qq_40244176/article/details/83062847
今日推荐
周排行