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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <fcntl.h>
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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章