#pragma once

#include <stdio.h>
#include <stdlib.h>

#include <curl/curl.h>

#ifdef    _DEBUG
#pragma comment(lib, "libcurld_imp.lib")
#else
#pragma comment(lib, "libcurl_imp.lib")
#endif

class CUrlInit
{
public:
    CUrlInit()
    {
        curl_global_init(CURL_GLOBAL_ALL);
    }
    
    ~CUrlInit()
    {
        curl_global_cleanup();
    }
    
protected:
    static CUrlInit m_Initialize;
};

CUrlInit CUrlInit::m_Initialize;

class CMyUrl
{
public:
    CMyUrl()
    {
        /* init the curl session */
        curl_handle = curl_easy_init();
    }

~CMyUrl()
    {
        /* cleanup curl stuff */
        curl_easy_cleanup(curl_handle);
    }

public:
    int get_file(const char* szUrl, const char* szFile)
    {
        curl_easy_reset(m_curl);
        
        /* set URL to get */
        curl_easy_setopt(curl_handle, CURLOPT_URL, szUrl);
        
        /* no progress meter please */
        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 files */         
        FILE* bodyfile = fopen(szFile, "wb");
        if (bodyfile == NULL) {
            curl_easy_cleanup(curl_handle);
            return -1;
        }
        
        /* we want the body be written to this file handle instead of stdout */
        curl_easy_setopt(curl_handle,   CURLOPT_WRITEDATA, bodyfile);
        
        /* get it! */
        curl_easy_perform(curl_handle);        
        
        /* close the body file */
        fclose(bodyfile);

return 0;
    }

protected:
    static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
    {
        int written = fwrite(ptr, size, nmemb, (FILE *)stream);
        return written;
    }

protected:
    CURL *curl_handle;
};

c/c++常用代码--使用libcurl下载文件的更多相关文章

  1. 使用libcurl下载文件小例

    libcurl是一个很强大的开源网络处理库,支持包括HTTP.HTTPS.FTP……一系列网络协议.用它来进行HTTP的get\post 或者下载文件更是小菜一碟,chrome内核都用到了它,本文主要 ...

  2. libcurl下载文件

    一.初始化 CURL *pHandler = curl_easy_init();   二.设置请求参数: 调用curl_easy_setopt方法,设置选项 curl_easy_setopt(pHan ...

  3. [C#常用代码]如何把指定文件夹中的文件移动到指定的文件夹

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. libcurl下载文件简易demo

    size_t test_save(void *ptr, size_t size, size_t nmemb, void *stream) { size_t sizes = size * nmemb; ...

  5. js发送post请求下载文件

    大家都知道ajax是不能直接下载文件的,所以一般都是通过一个超链接的形式去下载一个文件 但是当牵扯到需要发送很多数据到服务器上再下载的时候超链接的形式就有些太过勉强了 如下是一个工具方法(依赖jque ...

  6. 浏览器兼容性--IE11以及Edge等下载文件的中文名出现乱码,前后端解决方案

    项目中有用到文件下载功能,之前在处理下载时对IE浏览器下文件下载名进行过处理,测试也没有问题,但是功能上线后,业务反馈IE11文件下载文件名依然乱码.打印User-Agent字符串如下: IE11 U ...

  7. [转] 使用 JavaScript 创建并下载文件

    [From] https://gaohaoyang.github.io/2016/11/22/js-create-file-and-download/ 本文将介绍如何使用 JavaScript 创建文 ...

  8. 两种js下载文件的方法(转)

    function DownURL(strRemoteURL, strLocalURL){ try{ var xmlHTTP = new ActiveXObject("Microsoft.XM ...

  9. Ext下载文件

    项目中前台用的是Ext JS,要从数据库中查询数据并导出为Excel表格 对此研究了下,代码如下: 前台代码: /** * 进行下载文件(form方式) */ _downloadDraft:funct ...

随机推荐

  1. java中反射

    Person.java===>>person.class ==>>jvm中的类加载器===>>class对象:代表内存中Person.class ==>> ...

  2. 什么是动态语言 OC 的runtime

    OC是一门 动态语言. 问题来了.什么是动态语言? 与之相对的静态语言? 学习C++的时候,记得一个名词:“运行时的动态绑定”. 这个是 “多态”的概念. 简单提一下:关键:类指针可以指向本类,或者其 ...

  3. CSS3之弹性布局

    flexbox是CSS3提出的页面布局模块.flexbox可以把列表横向或者纵向排列,并且填满可以延伸到的空间.稍微复杂的布局可以通过嵌套flex container来实现. 利用flexbox可以方 ...

  4. plsql 登录后,提示数据库字符集(AL32UTF8)和客户端字符集(ZHS16GBK)不一致

    plsql 登录后,提示数据库字符集(AL32UTF8)和客户端字符集(ZHS16GBK)不一致 (2014-07-25 18:40:34)转载▼ 标签: it 分类: Database Databa ...

  5. RedHat安装VMwareTools出现解压压缩包时无法打开文件的现象

    出现这种情况的原因是因为解压命令没有加—C参数,使用的命令为:tat -xvzf VMware Tools: 正确的解压命令应该是: tar -xvzf VMware Tools -C /opt,加上 ...

  6. grub丢失的修复

    使用安装光盘进入rescure模式,经过配置后进入一个bashbash# grubgrub> root (hd0,6)grub> setup (hd0)重启即可

  7. Linux系统在嵌入式硬件上的移植

    =========================        基本常识       ========================= 一. Linux系统的基本构成: 1. loader 2.  ...

  8. PUTTY使用Ctrl+s僵死的问题

    算是分享个小经验吧! 一直都是使用VM+PUTTY的方式调试Linux程序,有时候在Vi中编辑了程序,Windowns下旧习难改,顺手就Ctrl+s了,尽管我知道Vi的保存是:w. 很不幸这时的PUT ...

  9. s3c6410_uart初始化及读写

    参考: 1)<USER'S MANUAL-S3C6410X>第31章 UART 2)u-boot uart初始化及读写:u-boot-x.x.x/board/samsumg/smdk641 ...

  10. [转]从两道经典试题谈C/C++中联合体(union)的使用

    宋宝华 21cnbao sweek@21cn.com 试题一:编写一段程序判断系统中的CPU是Little endian还是Big endian模式? 分析: 作为一个计算机相关专业的人,我们应该在计 ...