libcurl使用http、https下载文件

libcurl

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#include <string>
#include <functional>


struct MemoryStruct
{
    char *memory;
    size_t size;
};

using Callback = std::function<void(const MemoryStruct &m)>;

static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    size_t realsize = size * nmemb;
    struct MemoryStruct *mem = (struct MemoryStruct *)userp;
    mem->memory = (char*)realloc(mem->memory, mem->size + realsize);
    if (mem->memory == NULL) {
        /* out of memory! */
        printf("not enough memory (realloc returned NULL)\n");
        return 0;
    }
    memcpy(&(mem->memory[mem->size]), contents, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;
    return realsize;
}

class CurlDownload
{
public:
    CurlDownload(){}
    CURLcode Initialize()
    {
        CURLcode error;

        curl_global_init(CURL_GLOBAL_ALL);
        /* init the curl session */
        curl_handle = curl_easy_init();
        ///* specify URL to get */
        //curl_easy_setopt(curl_handle, CURLOPT_URL, const_cast<char*>(strUrl.c_str()));
        curl_easy_setopt(curl_handle, CURLOPT_URL, "http://10.66.91.15:7777/ld/smog/2612_src.jpg");

        /* send all data to this function */
        curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        /* we pass our 'chunk' struct to the callback function */
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);

        curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);


        /* some servers don't like requests that are made without a user-agent
        field, so we provide one */
        //curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");

        // https, skip the verification of the server's certificate.
        curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L);

        /* 设置连接超时,单位:毫秒 */
        curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT_MS, 1000L);

        // add by yexiaoyogn 10 second time out 
        curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, 2000);

        //add yexiaoyong set time out
        curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 3);
    }

    void SetCallback(Callback cb)
    {
        callback = cb;
    }

    CURLcode Download(std::string strUrl)
    {
        CURLcode res;

        /* specify URL to get */
        curl_easy_setopt(curl_handle, CURLOPT_URL, const_cast<char*>(strUrl.c_str()));
        //curl_easy_setopt(curl_handle, CURLOPT_URL, "http://10.66.91.15:7777/ld/smog/2612_src.jpg");

        chunk.size = 0;

        /* get it! */
        res = curl_easy_perform(curl_handle);
        /* check for errors */
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }
        else {
            /*
            * Now, our chunk.memory points to a memory block that is chunk.size
            * bytes big and contains the remote file.
            *
            * Do something nice with it!
            */
            printf("%lu bytes retrieved\n", (long)chunk.size);
        }

        callback(chunk);
    }

    void Finish()
    {
        /* cleanup curl stuff */
        curl_easy_cleanup(curl_handle);
        free(chunk.memory);
        /* we're done with libcurl, so clean it up */
        curl_global_cleanup();
    }

private:
    CURL *curl_handle;
    MemoryStruct chunk;
    Callback callback;
};



void CB(const MemoryStruct &m)
{
    static int cnt = 0;
    printf("CB: cnt:%d, %lu bytes retrieved\n", cnt++, (long)m.size);
}

void test()
{
    CurlDownload cd;
    cd.Initialize();
    cd.SetCallback(CB);
    while (true)
    {
        cd.Download("https://10.66.91.15/ld/smog/2612_src.jpg");
        cd.Download("http://10.66.91.15:7777/ld/smog/2612_src.jpg");
    }
    
    cd.Finish();
}

转载于:https://www.cnblogs.com/walkinginthesun/p/9600634.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值