zoukankan      html  css  js  c++  java
  • Ubuntu18.04.2 amd64编译安装libcurl支持https

    1.准备工作,安装ssl库:

    sudo apt-get install openssl
    sudo apt-get install libssl-dev

    2.下载libcurl源代码:

    wget https://curl.haxx.se/download/curl-7.65.3.tar.xz

    3.解压并进入源代码目录:

    tar xf curl-7.65.3.tar.xz
    cd curl-7.65.3

    4.配置编译选项:

    ./configure --prefix=/usr                           
                --disable-static                        
                --enable-threaded-resolver              
                --with-ca-path=/etc/ssl/certs
    

    5.编译:

    make
    

    6.安装(需要root权限):

    make install &&
    
    rm -rf docs/examples/.deps &&
    
    find docs ( -name Makefile* -o -name *.1 -o -name *.3 ) -exec rm {} ; &&
    
    install -v -d -m755 /usr/share/doc/curl-7.65.3 &&
    cp -v -R docs/*     /usr/share/doc/curl-7.65.3

    至此,编译安装完成。

    写代码测试一下:

    #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/");
     
        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if(res != CURLE_OK)
          fprintf(stderr, "curl_easy_perform() failed: %s
    ",
                  curl_easy_strerror(res));
     
        /* always cleanup */
        curl_easy_cleanup(curl);
      }
     
      curl_global_cleanup();
     
      return 0;
    }

    编译:

    gcc hello_https.c -l curl -o hello_https

    运行:

    ./hello_https

    运行结果截图:

     附 自定义回调处理响应数据代码:

    #include <stdio.h>
    #include <curl/curl.h>
    
    static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
    {
        if(stream == NULL) 
        return 0;
        printf("%s", ptr);
        return size * nmemb;
    }
    
    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/");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
     
        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if(res != CURLE_OK)
          fprintf(stderr, "curl_easy_perform() failed: %s
    ",
                  curl_easy_strerror(res));
     
        /* always cleanup */
        curl_easy_cleanup(curl);
      }
     
      curl_global_cleanup();
     
      return 0;
    }
    View Code
  • 相关阅读:
    获取Enum枚举值描述的几法方法
    Android开发入门 Button事件实现的方法(原创)
    最有价值的.Net第三方控件
    Eclipse快捷键大全(转载)
    recovery教程
    XP系统通过无线网卡共享宽带给其他设备,正确的共享设置(修正版,绝对可行)
    使用Eclipse写QT
    Android 4.0模拟器弹出“谷歌拼音输入法”已停止运行的解决方法
    C# 枚举用法总结
    谷歌安卓系统使用必读,什么是root, Recovery, Radio, APP TO SD, Rom
  • 原文地址:https://www.cnblogs.com/areful/p/11306651.html
Copyright © 2011-2022 走看看