libcurl上手笔记-Linux

操作系统:CentOS 7.6。

为了方便,直接使用yum安装:yum install libcurl-devel.

Installed:
  libcurl-devel.x86_64 0:7.29.0-59.el7_9.2                                                                                  

Dependency Updated:
  curl.x86_64 0:7.29.0-59.el7_9.2                             libcurl.x86_64 0:7.29.0-59.el7_9.2                            

可以看到安装的版本是7.29, 比最新版本8.71低。不过没关系,7.29够用了。

接下来就是测试demo了。

从官网现在examples,下载https.c:

#include <stdio.h>
#include <curl/curl.h>
 
int main(void)
{
  CURL *curl;
  CURLcode res;
 
  curl_global_init(CURL_GLOBAL_DEFAULT);
 
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com/");
 
#ifdef SKIP_PEER_VERIFICATION
    /*
     * If you want to connect to a site who is not using a certificate that is
     * signed by one of the certs in the CA bundle you have, you can skip the
     * verification of the server's certificate. This makes the connection
     * A LOT LESS SECURE.
     *
     * If you have a CA cert for the server stored someplace else than in the
     * default bundle, then the CURLOPT_CAPATH option might come handy for
     * you.
     */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
 
#ifdef SKIP_HOSTNAME_VERIFICATION
    /*
     * If the site you are connecting to uses a different host name that what
     * they have mentioned in their server certificate's commonName (or
     * subjectAltName) fields, libcurl refuses to connect. You can skip this
     * check, but it makes the connection insecure.
     */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
 
    /* cache the CA cert bundle in memory for a week */
    // 从7.87版本才开始支持这个选项,先注释掉
    //curl_easy_setopt(curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L);
 
    /* Perform the request, res gets the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));
 
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
 
  curl_global_cleanup();
 
  return 0;
}

直接在shell中使用命令行编译:

g++ https.c -lssl -lcrypto -lcurl  

由于libcurl-devel安装到系统目录下了,所以不需要指定头文件目录和库目录,直接连接就行了。在编译的过程中注释掉了一行代码 ,这是由于linux安装的库版本比example版本更低导致的。

   // 从7.87版本才开始支持这个选项,先注释掉
   //curl_easy_setopt(curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L);

 编译和运行都很顺利。

相关推荐

  1. libcurl上手笔记-Linux

    2024-04-02 15:36:04       38 阅读
  2. libcurl上手笔记-HTTP方法 GET、POST、PUT、DELETE

    2024-04-02 15:36:04       42 阅读
  3. Linux开发:Ubuntu22.04安装libcurl4

    2024-04-02 15:36:04       22 阅读
  4. libcurl test

    2024-04-02 15:36:04       41 阅读
  5. ts快速上手笔记01

    2024-04-02 15:36:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-02 15:36:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-02 15:36:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-02 15:36:04       82 阅读
  4. Python语言-面向对象

    2024-04-02 15:36:04       91 阅读

热门阅读

  1. 探索设计模式之单例模式:打造独一无二的实例

    2024-04-02 15:36:04       43 阅读
  2. js判断对象是否为空的几种方法

    2024-04-02 15:36:04       40 阅读
  3. JVM基础

    JVM基础

    2024-04-02 15:36:04      42 阅读
  4. 【C/C++】C语言实现单链表

    2024-04-02 15:36:04       35 阅读
  5. Vue 中的修饰符

    2024-04-02 15:36:04       35 阅读
  6. Vue3:使用Pinia存储、读取、修改数据

    2024-04-02 15:36:04       39 阅读
  7. 算法3:查找算法

    2024-04-02 15:36:04       40 阅读
  8. 每日一题 日期统计

    2024-04-02 15:36:04       38 阅读
  9. COMP2017 9017 Assignment 2

    2024-04-02 15:36:04       33 阅读