libcurl安装编译及QT配置https/http文件下载

1.curl下载

git clone https://github.com/curl/curl.git

2.curl的编译与安装

cd curl
./buidconf
./configure
make
make install

3.curl动态链接库与静态链接库查看

cd /usr/local/lib        ///动态库与静态库
cd /usr/local/include    ///头文件

4.QT中使用

1.创建工程应用;
2.在 .pro 文件中加上

LIBS += -lcurl

3.使用libcurl https下载

///////////
#include 
#include 
#include 
#include 
#include 
#include 
using std::string;
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
  size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
  return written;
}

int downloadurl(std::string URL,std::string localDirectory)
{
  CURL *curl_handle;
  static const char *pagefilename = localDirectory.c_str();
  FILE *pagefile;

  curl_global_init(CURL_GLOBAL_ALL);

  /* init the curl session */
  curl_handle = curl_easy_init();

  /* set URL to get here */
  curl_easy_setopt(curl_handle, CURLOPT_URL, URL.c_str());

  /* Switch on full protocol/debug output while testing */
  curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);

  /* disable progress meter, set to 0L to enable it */
  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 file */
  pagefile = fopen(pagefilename, "wb");
  if(pagefile)
  {
    /* write the page body to this file handle */
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
    /* get it! */
    curl_easy_perform(curl_handle);
    /* close the header file */
    fclose(pagefile);
  }
  /* cleanup curl stuff */
  curl_easy_cleanup(curl_handle);

  curl_global_cleanup();

  return 0;
}
void curlMainWindow::on_pushButton_clicked()
{
   downloadurl("https://img.zcool.cn/community/01ddc256eb71586ac7257d209712b7.jpg@1280w_1l_2o_100sh.jpg","./test.jpg");
}

///

5.curl卸载

apt-get remove curl

参考

https://blog.csdn.net/qianghaohao/article/details/51684862
https://blog.csdn.net/shmily138/article/details/85005195

你可能感兴趣的:(linux)