首页 > 其他分享 >libcurl 文件下载

libcurl 文件下载

时间:2022-09-05 19:11:32浏览次数:73  
标签:fp 文件 setopt libcurl CURLOPT easy curl 下载 size

直接上代码:

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

int http_get_upgrade_file(const char* file_name, const char* url)
{
    CURL *curl;
    CURLcode __attribute__ ((unused)) res;
    FILE *fp;
    curl = curl_easy_init();
    int httpcode = -1;
    if(curl)
    {
        fp = fopen(file_name,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        if(strstr(url,"https"))
        {
            curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
        }
        else
        {
            curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http");
        }
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60L);
        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 60L);
        res = curl_easy_perform(curl);
        if(fp)
        {
            fclose(fp);
        }
        curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode);
        curl_easy_cleanup(curl);
    }

    if (httpcode != 200)
    {
        printf("HttpCode error:%d try to check ip\n", httpcode);
        return -1;
    }
    return 0;
}

判断是HTTP还是https这里不太严谨,不过正常使用问题不大。

标签:fp,文件,setopt,libcurl,CURLOPT,easy,curl,下载,size
From: https://www.cnblogs.com/still-smile/p/16659244.html

相关文章

  • python a+模式读取文件时内容为空
    1.pythona+模式打开文件,然后直接读取,这时候读取出来的内容为空----为什么呢?这是因为打开时是以追加的模式打开的,这时候光标定位在最后,此时读取readline,吃从当前光标开......
  • vue 通过URL直接下载PDF文件而不是预览
    项目需求:用户在点击下载时下载文件。该项目为前后端分离项目,download不起作用。我在做这个功能点时使用的方法是直接window.location.href=url可在测试时却发现word等......
  • 官网CentOs7镜像下载详细步骤
    参考:https://blog.csdn.net/qq_15110681/article/details/121831182CentOs官网下载官网:https://www.centos.org/ 1. 官网进入后,点击Download。  2. 进去后,64......
  • iOS上架app store下载步骤
    1、安装iOS上架辅助软件Appuploader2、申请iOS发布证书(p12)3、申请iOS发布描述文件(mobileprovision)4、打包ipa5、上传ipa到iTunesConnect6、TestFlight方......
  • ipmitool for windows下载网址
    ipmitoolforwindows版本下载网址http://ipmiutil.sourceforge.net/......
  • 百度ueditor粘贴word图片且图片文件自动上传功能
    ​项目需求可发布文章需求涉及到富文本编辑器经过查阅我选择了较为简便不需要后端支持可独立完成的tinymce框架官方文档也是相当完整虽然都是全英文但是有强大的谷......
  • java下载文件的几种方式
    publicHttpServletResponsedownload(Stringpath,HttpServletResponseresponse){try{//path是指欲下载的文件的路径。Filefile=new......
  • 模块联邦-文件配置
    exposes-提供共享组件+constModuleFederationPlugin=require('webpack/lib/container/ModuleFederationPlugin');module.exports={//...configureWebpa......
  • (Spring)文件上传和下载
    文件上传的时候,浏览器将图片以MultipartFile的形式传到服务器,服务器将保存完的图片名响应给浏览器。文件下载的时候,浏览器收到图片名,再向服务器请求图片资源,服务器以流的......
  • Python读取txt文件
    F.read()和F.readlines():1#Python读取txt2defFread():3print('-----readby.read()-----')4withopen('test.txt',encoding='utf-8')asfile:5......