安装openssl使libcurl支持https的访问

首先如果你的操作系统没有事先安装过openssl,需要先下载安装openssl。

安装步骤如下:

wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
tar xvf openssl-1.1.1a.tar.gz 
cd openssl-1.1.1a/
./config 
make
sudo make install

编译安装完openssl后,进入curl-7.71.1的文件夹,输入:

./configure --prefix=$PWD/_include --with-ssl
make
make install

编译一段demo

#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>


#define true  1
#define false 0

typedef unsigned int bool;


size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
{
        char buf[10240] = {'\0'};
        strncpy(buf,ptr,1024);
        printf("=========================get Data=========================\n");
        printf("%s",buf);
}


bool postUrl()
{
        CURL *curl;
        CURLcode res;
        char *postString;

        char img1[12];
        char img2[12];
        char *key = "Y5SZESutYqLVjcr1zHiLf1";
        char *secret = "8a672a2a31134331b60ba2c219d0613a";
        int typeId =21;
        char *format = "xml";

        postString = (char *)malloc(strlen(key)+strlen(secret)+2048);
        sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
                        "","",key,secret,21,format);


        curl = curl_easy_init();
        if (curl)
        {
                curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // 指定cookie文件
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);    // 指定post内容
                curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");   // 指定url
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,readData);
               res = curl_easy_perform(curl);

                printf("OK:%d\n",res);
                curl_easy_cleanup(curl);
        }

        return true;
}

int main(void)
{
        postUrl();
}

 gcc demo.c -I ./curl-7.71.1/_include/include/ -L ./curl-7.71.1/_include/lib/ -lcurl

 编译成功,记得编译时要链接头文件和动态库。

猜你喜欢

转载自blog.csdn.net/aaaaaaaa123345/article/details/128686431