linux 下 libcurl 库的使用

libcurl 库的使用

libcurl主要功能就是用不同的协议连接和沟通不同的服务器。
libcurl当前支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议。
libcurl同样支持HTTPS证书授权,HTTP POST, HTTP PUT, FTP 上传, HTTP基本表单上传,代理,cookies,和用户认证。

1 、 源码下载

https://github.com/curl/curl

2、编译安装

#./configure –enable-shared –enable-static –prefix=/usr/local/

#make

#make install

3、查看

# curl -V
curl 7.50.2 (x86_64-pc-linux-gnu) libcurl/7.50.2 OpenSSL/1.0.1e zlib/1.2.3 libidn/1.18
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: IDN IPv6 Largefile NTLM NTLM_WB SSL libz UnixSockets

4、代码测试


main.hpp

#ifndef main_hpp
#define main_hpp

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#define ERROR_LOG(fmt,args...) {printf("\n[%s(line:%d)]:" fmt,__FILE__, __LINE__,##args);fflush(stdout);}
#define DEBUG_LOG(fmt,args...)  {printf("\n[%s(line:%d)%ld]:" fmt,__FILE__, __LINE__,time(NULL),##args);fflush(stdout);}
#define WARNING_LOG(fmt,args...)  {printf("\n[%s(line:%d)]:" fmt,__FILE__, __LINE__,##args);fflush(stdout);}
#define TRACE_LOG(fmt,args...)  {printf("\n[%s(line:%d)]:" fmt,__FILE__, __LINE__,##args);fflush(stdout);}
#define INFO_LOG(fmt,args...)  {printf("\n[%s(line:%d)]:" fmt,__FILE__, __LINE__,##args);fflush(stdout);}


#endif

main.cpp

#include"main.hpp"
#include"curlDownload.hpp"

int main(int argc, char **argv)
{
 if (argc != 3)
 { 
   ERROR_LOG("input command param error");
   return -1;
 }
 curlDownload mydownload;
 mydownload.download(argv[1],0,argv[2]);
 return 0;
}

curlDownload.hpp

//
//  curlDownload.hpp
//  
//
//  Created by jianjian Qi on 9/9/16.
//  Copyright © 2016 tgbtgb. All rights reserved.
//

#ifndef curlDownload_hpp
#define curlDownload_hpp

#include <stdio.h>
#include <curl/curl.h>
#include "main.hpp"


static size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);

class curlDownload
{

 friend size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);

public:

  int download(char * sourceUrl, long long totalSize, char * saveFile);

private:

  char szSourceUrl[1024];
  char szSaveFile[1024];

  FILE * fp;
  long long llCurrentSize;
  long long llTotoalSize;
  CURL *curl;
};

#endif /* curlDownload_hpp */

curlDownload.cpp

//
//  curlDownload.cpp
//  
//
//  Created by jianjian Qi on 9/9/16.
//  Copyright © 2016 tgbtgb. All rights reserved.
//

#include "curlDownload.hpp"

size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
  if (userdata == NULL)
  {
    ERROR_LOG("write_callback userdata error");
    return -1;
  }

  curlDownload * download = (curlDownload *)userdata;
  size_t res = fwrite(ptr, size, nmemb, download->fp);
  if (res <= 0)
  {
    ERROR_LOG("[%s] write(%lld) faild@%s",download->szSourceUrl,download->llCurrentSize,strerror(errno));
    return -1;
  }

  download->llCurrentSize += res;
  return res;
}

int curlDownload::download(char * sourceUrl, long long totalSize, char * saveFile)
{

  CURLcode res;
  int ret = -1;
  fp      = NULL;
  llCurrentSize = 0;
  llTotoalSize  = totalSize;

  memset(szSourceUrl,0,sizeof(szSourceUrl));
  memset(szSaveFile,0,sizeof(szSaveFile));

  memcpy(szSourceUrl, sourceUrl, strlen(sourceUrl));
  memcpy(szSaveFile, saveFile, strlen(saveFile));

  do
  {
    fp = fopen(szSaveFile,"wb");

    if (fp == NULL)
    {
      ERROR_LOG("[%s] open %s faild@%s",szSourceUrl, szSaveFile, strerror(errno));
      break;
    }

    /* get a curl handle */
    curl = curl_easy_init();

    if (!curl)
    {
      ERROR_LOG("[%s] curl_easy_init error@%s",szSourceUrl,curl_easy_strerror(res));
      break;
    }

    res = curl_easy_setopt(curl, CURLOPT_URL, szSourceUrl);

    if (CURLE_OK != res)
    {
      ERROR_LOG("[%s] CURLOPT_URL error@%s",szSourceUrl,curl_easy_strerror(res));
      break;
    }

    res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);

    if (CURLE_OK != res)
    {
      ERROR_LOG("[%s] CURLOPT_WRITEFUNCTION error@%s",szSourceUrl,curl_easy_strerror(res));
      break;
    }

    res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);

    if (CURLE_OK != res)
    {
      ERROR_LOG("[%s] CURLOPT_WRITEDATA error@%s",szSourceUrl,curl_easy_strerror(res));
      break;
    }

    res = curl_easy_perform(curl);

    if (CURLE_OK != res)
    {
      ERROR_LOG("[%s] curl_easy_perform error@%s",szSourceUrl,curl_easy_strerror(res));
      break;
    }

    /* download succ */
    INFO_LOG("[%s] download succ and save as %s", szSourceUrl, szSaveFile);
    ret = 0;

  } while(0);

  if (fp == NULL)
  {
    return ret;
  }

  fclose(fp);
  fp = NULL;

  if (curl)
  {
    curl_easy_cleanup(curl);
    curl = NULL;
  }

  return ret;

}

5、编译

#g++ main.cpp curlDownload.cpp -lcurl

6、测试

#./a.out https://github.com/curl/curl/archive/master.zip master.zip

参考网址:https://curl.haxx.se/libcurl/c/

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值