Linux C/C++实现https post/get请求

2023-05-16

Linux C/C++要实现https访问借助于libcurl工具,如果是实现自定义SSL证书通信校验的,可以通过C调用openssl来实现,也可以通过libcurl来。记得linux上提前安装好openssl库。

1.编译安装libcurl

curl库的代码下载地址:https://curl.haxx.se/download.html

第一步:进入curl工程目录执行./buidconf产生configure配置文件;

第二步:执行产生的configure脚本: ./configure

第三步:make

第四步:sudo make install

2.到/usr/local/lib/即可查看到安装好的库文件

<s /usr/local/lib250;48;5                          
libcurl.a   libcurl.so    libcurl.so.4.5.0  python2.7  python3.6
libcurl.la  libcurl.so.4  pkgconfig         python3.5

3.编写https代码:

#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\n",
              curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }

  curl_global_cleanup();

  return 0;
}

4.编译运行:

gcc https.c -l curl -o https

./https

5.libcurl还可以更多的功能,具体可以参考

https://curl.haxx.se/libcurl/c/example.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Linux C/C++实现https post/get请求 的相关文章

随机推荐